使用一个 AsyncTask 无法显示进度对话框 class

Progress Dialog impossible to show using one AsyncTask class

我试图在我的 MainActivity 中显示一个进度对话框,但这是不可能的: 这个 class 在我的 MainActivity 里面:

   class MyAsyncTask extends AsyncTask<Void, Integer, Void> {

        ProgressDialog progressDialog;
        private String titulo;
        private String mensaje;
        private MainActivity activity;
        public MyAsyncTask(MainActivity activity){
            this.activity = activity;

        }

        @Override
        protected Void doInBackground(Void... voids) {
            this.activity.createDirs();
            this.activity.copyAssets();
            return null;
        }

        @Override
        protected void onPreExecute() {

            this.progressDialog = new ProgressDialog(MainActivity.this);
            this.progressDialog.setTitle("test");
            this.progressDialog.setMessage("test message...");
            this.progressDialog.show();
            Toast.makeText(MainActivity.this, "OPA", Toast.LENGTH_LONG).show();
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            progressDialog.dismiss();
        }

    }

这段代码在我的 MainActivity.java 中:基本上,当用户按下一个按钮时,我会创建一个对我的 MyAsyncTask 的引用并执行它。

btnInit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(flag==1){
                    myAsyncTask = new MyAsyncTask(new MainActivity());
                    myAsyncTask.execute();
                    try {
                        myAsyncTask.get();
                    }catch (Exception e){

                    }

                    Intent intent = new Intent(MainActivity.this, Dashboardactivity.class);
                    startActivity(intent);
                    flag=0;
                }else{
                    Toast.makeText(MainActivity.this,"error", Toast.LENGTH_LONG).show();

                }
            }

class MyAsyncTask 的方法 doInBackground() 的代码正在执行,没有问题。我将不胜感激任何想法,非常感谢。

您的线程被阻塞,当您调用 myAsyncTask.get() 时,这就是您的进度对话框未显示的原因,因此删除 myAsyncTask.get() 并将这些行添加到 onPostExecute

Intent intent = new Intent(MainActivity.this, Dashboardactivity.class);
                    startActivity(intent);

完整代码:

  btnInit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(flag==1){
                        myAsyncTask = new MyAsyncTask(new MainActivity());
                        myAsyncTask.execute();
  
                        flag=0;
                    }else{
                        Toast.makeText(MainActivity.this,"error", Toast.LENGTH_LONG).show();
    
                    }
                }

在你的 asyncTask 上:

   class MyAsyncTask extends AsyncTask<Void, Integer, Void> {

        ProgressDialog progressDialog;
        private String titulo;
        private String mensaje;
        private MainActivity activity;
        public MyAsyncTask(MainActivity activity){
            this.activity = activity;

        }

        @Override
        protected Void doInBackground(Void... voids) {
            this.activity.createDirs();
            this.activity.copyAssets();
            return null;
        }

        @Override
        protected void onPreExecute() {

            this.progressDialog = new ProgressDialog(MainActivity.this);
            this.progressDialog.setTitle("test");
            this.progressDialog.setMessage("test message...");
            this.progressDialog.show();
            Toast.makeText(MainActivity.this, "OPA", Toast.LENGTH_LONG).show();
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            progressDialog.dismiss();
            Intent intent = new Intent(MainActivity.this, Dashboardactivity.class);
            startActivity(intent);
        }

    }