0

我有一个设置在 AsyncTask 内的进度圈。当 asynctask 正在执行时,它会显示大约一秒钟,然后消失。一旦任务完成,如果我按下后退按钮,圆圈会显示很长时间。为什么是这样?

private class AsyncGetRota extends AsyncTask<String, Void, Void> {

        ProgressDialog progressDialog;


        @Override
        protected void onPreExecute()
        {
            progressDialog= ProgressDialog.show(NfcscannerActivity.this, 
                    "Connecting to Server"," retrieving rota...", true);

            //do initialization of required objects objects here                
        };      


        @Override
        protected Void doInBackground(String... params) {



            try {
                Log.e(TAG, "inside doInBackground");
                rotaArray = nfcscannerapplication.loginWebservice.getRota(params[0], params[1]);

                cancel(true);



            } catch (Exception e) {
               e.printStackTrace();
            }
            return null;

        }

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



    }

[更新]

getRota.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Log.e(TAG, "onclicked getRota");

                    String[] params = new String[]{"36", "18-09-2012"}; 
                    AsyncGetRota agr = new AsyncGetRota();
                    agr.execute(params);

                    for(int i = 0; i < 60; i++){

                        if(agr.isCancelled() == true){
                            Log.e(TAG, "asyncTask is finished");
                            break;
                        }


                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }//end of for loop

                    Intent intent = new Intent(NfcscannerActivity.this,
                            GetRota.class);

                    Bundle b = new Bundle();
                    b.putSerializable("rotaArray", rotaArray);

                    intent.putExtra("rotaArrayBundle", b);
                    startActivity(intent);

                }// end of onclick
            });
4

3 回答 3

0
...
new MyAsyncTask().execute(string);
...
}

class MyAsyncTask extends AsyncTask<String, Void, Whatever > {
    ...
    @Override
    protected Whatever doInBackground(String... params) {

       Log.e(TAG, "inside doInBackground");
       rotaArray = nfcscannerapplication.loginWebservice.getRota(params[0],       params[1]);
       return rotaArray;
    }

    @Override
    protected void onPostExecute(Whatever result)
    {
        super.onPostExecute(result);
        if(progressDialog != null)
        progressDialog.dismiss();

        Intent intent = new Intent(NfcscannerActivity.this, GetRota.class);
        Bundle b = new Bundle();
        b.putSerializable("rotaArray", result);

        intent.putExtra("rotaArrayBundle", b);
        startActivity(intent);
    }
}

启动 AsyncTask 后,您应该让执行继续,而不是使用某些循环或其他东西阻止它。

于 2012-09-21T14:17:30.000 回答
0

尝试像这样实现它:

protected void onPreExecute() {
   dialog = new ProgressDialog(activity);
   dialog.setMessage("Processing...");
   dialog.show();
}

protected void onPostExecute(Void result) {
   if (dialog.isShowing()) {
       dialog.dismiss();
   } 
};

这总是对我有用

于 2012-09-21T14:07:46.163 回答
0

这里有几个问题,你不初始化 ProgressDialog,初始化一个像这样初始化 ProgressDialog 的构造函数......

 public AsyncGetRota(Activity activity) {
        this.activity = activity;
        dialog = new ProgressDialog(activity);
    } 

然后在 onPostExecute 检查你的 ProgressDialog 是否为空,像这样

@Override
    protected void onPostExecute(Void result)
    {
        super.onPostExecute(result);
        if(progressDialog != null)
        progressDialog.dismiss();
    }
于 2012-09-21T14:14:35.793 回答