Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async task to show an AlertDialog

For the past few days, I haven't been able to solve an issue with my dialog box. I am running a thread to show the dialog box for 5000ms and removing it. and I am trying to show a toast("SUCCESS"). The problem is I am getting the dialog box for the second time also. I am new to android development and I need to solve this with Async Task so that I can delay the second thread and show a alert dialog.builder with a positive button instead of toast. I goggled a lot but I confused to to implement this

Here I am sending my credentials to server and while sending I am showing a progress dialog box for 5000ms and I want to have a separate thread in order to show the dialog.builder with a positive button.( When the user get a response in the logcat for that I should check the responsecode==1 or not from the logcat to show the builder)

plz someone help me to solve this with some code snippet if possible.

Thanks in advance.

this is the code where I need to implement Async task

Button signin = (Button) findViewById(R.id.regsubmitbtn);
    signin.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // showDialog(0);
            t = new Thread() {
                public void run() {
                    register();
                    try {
                        while (counter < 2) {
                            showmsg(0);
                            Thread.sleep(5000);
                            removeDialog(0);
                            // Toast.makeText(Register.this, "Registerd", Toast.LENGTH_LONG).show();
                            showmsg(1);
                            // toast.show();

                            Thread.sleep(1000);

                        }

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            t.start();
        }
    });

}

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case 0: {
        ++counter;
        dialog = new ProgressDialog(this);
        if (counter == 1) {
            dialog.setMessage("Registering...");
        }
        else {
            String resultsRequestSOAP = "";
            if (Log.v("TAG", String.valueOf(resultsRequestSOAP)) == 1)
                ;
            {
                Context context = getApplicationContext();
                CharSequence text = "Registerd";
                int duration = Toast.LENGTH_LONG;

                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
            }
        }
        dialog.setIndeterminate(true);
        dialog.setCancelable(true);
        return dialog;
    }
    }
    return null;
}

public void showmsg(int actionsToBePerformedOnScreen) {
    Message msg = new Message();
    msg.what = actionsToBePerformedOnScreen;
    handler.sendMessage(msg);

}

public Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case 0:

            showDialog(0);

            break;

        case 1:
            // clear all images in the list
            removeDialog(0);
            break;

        }

    };
};
like image 795
Randroid Avatar asked Jun 27 '11 13:06

Randroid


2 Answers

Easy: show dialog onPreExecute, register() in doInBackground and hide dialog in onPostExecute. Finally, do new RegisterTask().execute() in your onclick.

 private class RegisterTask extends AsyncTask<Void, Void, Boolean> {
  private final ProgressDialog dialog = new ProgressDialog(YourClass.this);

  protected void onPreExecute() {
     this.dialog.setMessage("Signing in...");
     this.dialog.show();
  }

  protected Boolean doInBackground(final Void unused) {
     return Main.this.register(); //don't interact with the ui!
  }

  protected void onPostExecute(final Boolean result) {
     if (this.dialog.isShowing()) {
        this.dialog.dismiss();
     }
     if (result.booleanValue()) {
     //also show register success dialog
     }
  }
}
like image 168
dmon Avatar answered Sep 28 '22 07:09

dmon


Why you don't use Android AsyncTask? For example:

public class MyPreloader extends AsyncTask<InputObject, Void, OutputObject>{
private Context context;
private ProgressDialog dialog;

public MyPreloader(Context context){
    this.context = context;
}

@Override
protected void onPreExecute() {
    dialog = new ProgressDialog(context);
    dialog.setMessage("Please wait...");
    dialog.setIndeterminate(true);
    dialog.show();
    super.onPreExecute();
}   

@Override
protected ResponseBase doInBackground(InputObject... params) {
InputObject input = params[0]; 
//some code for background work
    }

@Override
protected void onPostExecute(OutputObject result) {
    if (dialog.isShowing()) {
        dialog.dismiss();
    }
    super.onPostExecute(result);
}
like image 44
Dmytro Boichenko Avatar answered Sep 28 '22 06:09

Dmytro Boichenko