Android AlertDialog inside AsyncTask

前端 未结 4 1746
隐瞒了意图╮
隐瞒了意图╮ 2021-01-01 05:57

I have a listview which have checkboxes. For each checkbox (they are about 3), it has a specific AsyncTask for it.

I never know what checkboxes use

4条回答
  •  情深已故
    2021-01-01 06:28

    alert dialog is foreground thing so it can not be done in background method of async task. Do it by this way

    private class showMessageAsync extends AsyncTask {
         AlertDialog alertDialog;
         protected void onPreExecute() {
        super.onPreExecute();
                alertDialog = new AlertDialog.Builder(YourClasss.this);  
         }
         @Override
         protected String doInBackground(Void... params){       
                return null;
         }
         @Override
         protected void onPostExecute(String result) {
                super.onPostExecute(result);
    
                alertDialog.setTitle("The Process");  
                alertDialog.setIcon(R.drawable.success);
                alertDialog.setCanceledOnTouchOutside(false);
                alertDialog.setMessage("All done!");  
                alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                                      new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog, int which) {
                                                    Intent A = new Intent(DownloadActivity.this, Menu_activity.class);
                                                    startActivity(A);
                                                    finish();
                                            }
                                        });
                alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {          
                                      @Override
                                      public void onDismiss(DialogInterface dialog) {
                                                Intent A = new Intent(DownloadActivity.this, Menu_activity.class);
                                                startActivity(A);
                                                finish();
                                      }
                        });
                alertDialog.show();
        }
    
    }
    

提交回复
热议问题