0

In my app, i have an asynchronous task that will download a file from a remote server. In that class, I have set a ProgressDialog that has a cancel button. It could have been easier if I just use that async task once and by creating and instance of the AsyncTask. I could have just call asyncTask.cancel(true), as what I saw on some samples.

But in my Activity, i have executed that AsyncTask many times like this:

new MyAsynTask().execute(url_of_file_1); 
new MyAsynTask().execute(url_of_file_2);
new MyAsynTask().execute(url_of_file_3);

Though this isn't exactly the structure but the sense is something like this.

This is how the MyAsyncTask looks like:

        private class MyAsyncTask extends AsyncTask<String, Integer, String> {
            ProgressDialog pdialog;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                if(!MyActivity.this.isFinishing()){
                    mProgressDialog = new ProgressDialog(MyActivity.this);
                    mProgressDialog.setCancelable(false);
                    mProgressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Cancel", new CancelOnClickListener());
                 }
            }
            @Override
            protected String doInBackground(String... fileUrl) {

                try {

                    //do connection to url things
                    if(!isCancelled()){
                        // download the file

                    }else{
                        pdialog.dismiss();
                        mProgressDialog.dismiss();
                    }
                }catch(MalformedURLException e){

                }catch(FileNotFoundException e){

                }catch(Exception e){

                }
                return null;

            }


            @Override
            protected void onProgressUpdate(Integer... values) {

                if(!MyActivity.this.isFinishing()){
                    mProgressDialog.setMessage("Downloading..." + values[0]);
                    mProgressDialog.show();
                    super.onProgressUpdate(values);
                }
            }
            @Override
            protected void onPostExecute(String result) {
                if(!MyActivity.this.isFinishing()){
                    super.onPostExecute(result);
                    mProgressDialog.dismiss();

                }
            }

            private final class CancelOnClickListener implements DialogInterface.OnClickListener {

                public void onClick(DialogInterface dialog, int which) {
                    new MyAsyncTask().cancel(true); // is this correct?
                    MyActivity.this.finish();
                    try {
                        MyActivity.this.finalize();
                    } catch (Throwable e) {
                        application.shortToast("Error encountered in closing the page");
                    }
                    // maybe i should call super.onBackPressed();
                    application.shortToast("Download Cancelled");
                }
            }
        }

What happened when i press cancel is that it always have this exception, but my app won't crash sometimes though...

    08-14 23:07:12.702: E/WindowManager(8623): Activity com.android.app.MyActivityhas leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@43f7c938 that was originally added here
    08-14 23:07:12.702: E/WindowManager(8623): android.view.WindowLeaked: Activity com.android.app.MyActivityhas leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@43f7c938 that was originally added here
    08-14 23:07:12.702: E/WindowManager(8623):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:384)
    08-14 23:07:12.702: E/WindowManager(8623):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292)
    08-14 23:07:12.702: E/WindowManager(8623):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
    08-14 23:07:12.702: E/WindowManager(8623):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
    08-14 23:07:12.702: E/WindowManager(8623):  at android.view.Window$LocalWindowManager.addView(Window.java:547)
    08-14 23:07:12.702: E/WindowManager(8623):  at android.app.Dialog.show(Dialog.java:285)
    08-14 23:07:12.702: E/WindowManager(8623):  at com.android.app.MyActivity$MyAsyncTask.onPreExecute(PartsActivity.java:784)
    08-14 23:07:12.702: E/WindowManager(8623):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
    08-14 23:07:12.702: E/WindowManager(8623):  at android.os.AsyncTask.execute(AsyncTask.java:534)
    08-14 23:07:12.702: E/WindowManager(8623):  at com.android.app.MyActivity$SecondAsyncTask.onPostExecute(PartsActivity.java:980)
    08-14 23:07:12.702: E/WindowManager(8623):  at com.android.app.MyActivity$SecondAsyncTask.onPostExecute(PartsActivity.java:1)
    08-14 23:07:12.702: E/WindowManager(8623):  at android.os.AsyncTask.finish(AsyncTask.java:631)
    08-14 23:07:12.702: E/WindowManager(8623):  at android.os.AsyncTask.access$600(AsyncTask.java:177)
    08-14 23:07:12.702: E/WindowManager(8623):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)

. . . .

So what is the proper way of ending those excuted AysnTask when i press the cancel button in my ProgressDialog?

4

1 回答 1

1

保留每个任务的引用: MyAsynTask task1 = new MyAsynTask(); task1.execute(url_of_file_1);

由于您的异常具有其他性质,因此您需要在单击作为事件线程的取消按钮时关闭对话框,而不是在后台线程中。在您的取消按钮回调中: myDialog.dissmiss() task1.cancel(true);

于 2013-08-15T00:39:25.903 回答