peter
peter

Reputation: 1

Show AlertDialog after background thread executed succefully

I am running a background thread. After background thread executed succesfully, I want to show some alert message to the user on UI.

Upvotes: 0

Views: 2601

Answers (4)

if you worked with AsynTask then you can show it in onPostExecute(). http://www.mysamplecode.com/2011/09/android-asynctask-httpclient-with.html

AlertDialog alertDialog = new AlertDialog.Builder(
                        AlertDialogActivity.this).create();


    // Setting Dialog Title
    alertDialog.setTitle("Alert Dialog");

    // Setting Dialog Message
    alertDialog.setMessage("Welcome to AndroidHive.info");

    // Setting OK Button
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            // Write your code here to execute after dialog closed
            Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
            }
    });

    // Showing Alert Message
    alertDialog.show();

for more help with alert see http://www.androidhive.info/2011/09/how-to-show-alert-dialog-in-android/

Upvotes: 2

Pavankumar Vijapur
Pavankumar Vijapur

Reputation: 1206

Launch an Async thread. The async thread provides you with the three methods : OnPreExecute(), doInBackground() and onPostExecute().

The first and last methods are called on the UI thread, So once the operation in doInBackground

Upvotes: 0

Sreedev
Sreedev

Reputation: 6653

Its not impossible to connect the background thread to with the UI.With the help of a handler you can send messages.By checking that messages you can show the alert messages.i think this piece of code will help you.

Thread animator = new Thread() {
        public void run() {
            int i = 0;
            try {
                sleep(4000);
                while (i < 4) {
                    sleep(50);
                    handler.sendMessage(handler.obtainMessage(i));
                    i++;
                }
            } catch (Exception e) {

            }
        }
    };
    animator.start();
    handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                animatedimage.setImageResource(R.drawable.sub1);
            } else if (msg.what == 1) {
                animatedimage.setImageResource(R.drawable.sub2);
            } else if (msg.what == 2) {
                animatedimage.setImageResource(R.drawable.sub3);
            } else if (msg.what == 3) {
                animatedimage.setImageResource(R.drawable.sub4);
            }
        }

    };

If you are using a Assync tasy you can do it in

onPostExecute()

Upvotes: 0

Akhilesh B Chandran
Akhilesh B Chandran

Reputation: 6608

If you are using AsyncTask, then you could write the code for displaying the message (maybe a Toast), in onPostExecute().

Upvotes: 0

Related Questions