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();
   }
});

Then callin AsynTask like :

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

i am returning value which url responding me on the base of responce i am calling second url but the problem it that when i am returning value from async task in this way it will not display an dialogbox and it will hang device till asynTask cannot complete its task so can any one help me.

PankajAndroid
  • 2,689
  • 3
  • 27
  • 41

2 Answers2

4

In here:

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

you are calling AsyncTask.get() method on UI Thread instead of on other thread so this will freeze UI until doInBackground execution is not completed and then get result back to main ui Thread.

Option 1#

you will need to use onPostExecute for updating UI when doInBackground execution complete because onPostExecute always called on UI thread. for showing ProgressDialog you can use onPreExecute() which called before stating background task

Option 2#

call AsyncTask.get() inside a Thread for getting results from doInBackground and use Activity.runOnUiThread for updating UI from non UI Thread

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
0

Try this :

 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); 
            }
        }

for the fisrt call of your AsyncTask :

SendingWebPageTask sendMaster=new SendingWebPageTask(true);//passing true , so the asyncTask will launch another asynctask for the secondURL retreived from onPostExecute()
sendMaster.execute(UrlMaster);
Houcine
  • 24,001
  • 13
  • 56
  • 83
  • i had tried this also but on the base of calling first url i am returning value and on return value i am calling second url in this way i have to return value from asynk task so this solution doesnt work in my case – PankajAndroid May 23 '13 at 10:59
  • so , in the `onPostExecute()` method you can execute your `AsyncTask` again, and pass to it the second url that you got from the first call of the asyncTask. see my edits – Houcine May 23 '13 at 11:00
  • i am continiosly sending data to web server when i call url it will give me reply if it is successfuly store then i am calling second or third url in this way how can i use onPostExecute()?? – PankajAndroid May 23 '13 at 11:03
  • Thanksf for this but i have more data inshort i am retriving data from my sqlite and storing on webserver so when i store 1st data it will respond me. on the base of respoce i am sending next to next data – PankajAndroid May 23 '13 at 11:09
  • all that treatment you can do it into onPostExecute, you can test your result and check if you should execute the AsyncTask again ,with the next data to send to server, or not – Houcine May 23 '13 at 11:14