1
final Dialog Alert_Dialog = new Dialog(MainList.this);
Alert_Dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
Alert_Dialog.setContentView(R.layout.alert_dialog_progressbar);
((TextView)Alert_Dialog.findViewById(R.id.txtMessage)).setText(R.string.please_wait_);
Alert_Dialog.setCancelable(false);
Alert_Dialog.show();
((Button) Alert_Dialog.findViewById(R.id.btnCancel)).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    Alert_Dialog.cancel();
   }
});

然后调用 AsynTask 像:

String res="";
try{
    SendingWebPageTask sendMaster=new SendingWebPageTask();
    res=sendMaster.execute(UrlMaster).get();
}catch (Exception e) {
e.printStackTrace();
}

我正在返回基于响应的 url 响应我的值我正在调用第二个 url 但问题是当我以这种方式从异步任务返回值时它不会显示对话框并且它将挂起设备直到 asynTask 无法完成它任务所以任何人都可以帮助我。

4

2 回答 2

4

在这里:

res=sendMaster.execute(UrlMaster).get(); //<<<

您在 UI 线程而不是其他线程上调用AsyncTask.get()doInBackground方法,因此这将冻结 UI 直到执行未完成,然后将结果返回到主 ui 线程。

选项1#

当 doInBackground 执行完成时,您将需要onPostExecute用于更新 UI,因为 onPostExecute 总是在 UI 线程上调用。显示 ProgressDialog 您可以onPreExecute()在说明后台任务之前使用 which call

选项2#

在线程内调用AsyncTask.get()以从 doInBackground 获取结果并使用 Activity.runOnUiThread从非 UI 线程更新 UI

于 2013-05-23T10:54:51.820 回答
0

尝试这个 :

 class SendingWebPageTask extends AsyncTask<String, Void, String>
        {
                private Dialog alertDialog;
                private boolean shouldCallTheSecondURL = false;

                public SendingWebPageTask(boolean shouldCallSecondURL) {
                     this.shouldCallSecondURL = shouldCallSecondURL;
                }
            @Override
            protected void onPreExecute() {
               alertDialog = new Dialog(MainList.this);
    alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    alertDialog.setContentView(R.layout.alert_dialog_progressbar);
    ((TextView)alertDialog.findViewById(R.id.txtMessage)).setText(R.string.please_wait_);
    alertDialog.setCancelable(false);
    alertDialog.show();
    ((Button) alertDialog.findViewById(R.id.btnCancel)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        alertDialog.dismiss();
       }
    });
            }

            @Override
            protected String doInBackground(String... params) {
                //TODO your code in background...
                            return result;//result is in String format
            }

            @Override
            protected void onPostExecute(String result) {
                            //dismiss the alertDialog after the task finish his background work
                            alertDialog.dismiss();
                super.onPostExecute(result);
                Log.d("SendingWebPageTask", "result task : "+result);
                            String secondUrl = result;
                            if(shouldCallSecondURL)
                                new SendingWebPageTask(false).execute(secondUrl); 
            }
        }

对于您的第一个电话AsyncTask

SendingWebPageTask sendMaster=new SendingWebPageTask(true);//passing true , so the asyncTask will launch another asynctask for the secondURL retreived from onPostExecute()
sendMaster.execute(UrlMaster);
于 2013-05-23T10:56:58.420 回答