如何阻止Asynctask

时间:2012-08-16 05:45:39

标签: android

我想在单击按钮(cancelsync)时取消Asynctask,但是执行onProgressUpdate()方法并显示错误:活动已泄漏窗口,因为它在活动更改后尝试执行alert.show()。那么我可以做些什么来阻止这个AsyncTask 单击cancelsync后

CancelButton:

 cancelsync.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    AlertDialog alert =new AlertDialog.Builder(NeliteMusicActivity.this).create();
                    alert.setTitle(R.string.set4Play);
                    alert.setMessage(getString(R.string.interrupted));
                    alert.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                    alert.setButton(getString(R.string.yes), new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int which) {
                              if(communicator != null && !communicator.isCancelled())
                              communicator.cancel(true);
                              Intent myintent = new Intent(NeliteMusicActivity.this,HomeScreen.class);
                              startActivity(myintent);
                            return;
                        } 
                        }); 

                    alert.setCanceledOnTouchOutside(false);
                    alert.show();
                    isTrue = false;
                }
            });

AsyncTask:


       class Communicator extends AsyncTask<String, String, String> 
        {
        @Override
                protected void onProgressUpdate(String... values) {
                    super.onProgressUpdate(values);
                    int a=messag;
                    progress.setProgress(a);
                    textsync.setText("Syncing mix "+c+"/"+list.size());
                    if(progress.getMax()<=a)
                    {
                                if (isOnline()) 
                                {
                                    final AlertDialog alert =new AlertDialog.Builder(NeliteMusicActivity.this).create();
                                    alert.setTitle(R.string.set4Play);
                                    alert.setMessage(getString(R.string.updatedMix));
                                    alert.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                                    alert.setButton(getString(R.string.ok), 
                                            new DialogInterface.OnClickListener() 
                                    {
                                          public void onClick(DialogInterface dialog, int which) {
                                              alert.dismiss();
                                              animatedStartActivity();
                                        }
                                        });
                                    alert.setCanceledOnTouchOutside(false);
                                    alert.show();
                                }
                            if (!isOnline()) {
                                final AlertDialog alert =new AlertDialog.Builder(NeliteMusicActivity.this).create();
                                alert.setTitle(R.string.set4Play);
                                alert.setMessage(getString(R.string.connectionerror));
                                alert.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                                alert.setButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
                                      public void onClick(DialogInterface dialog, int which) {
                                          alert.dismiss();
                                          animatedStartActivity();
                                    } 
                                    }); 
                                alert.setCanceledOnTouchOutside(false);
                                alert.show();
                            }

                    }
        }

1 个答案:

答案 0 :(得分:0)

在您正在使用的情况下,

communicator.cancel(true); // this is working fine

您收到错误,

  

错误:当活动尝试执行alert.show()时,活动已泄露窗口   活动结束后。

正如错误所述,这是因为您尝试显示AlertDialogActivity不是Foreground