Android AsyncTask返回值

时间:2013-05-23 10:48:58

标签: android android-asynctask

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无法完成任务,任何人都可以帮助我。

2 个答案:

答案 0 :(得分:4)

在这里:

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

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

选项1#

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

选项2#

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

答案 1 :(得分: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);