简体   繁体   中英

How can I show a Dialog box from a thread other than the UI thread

My app requires gps reading, so on the main thread I start a Thread that reads the GPS but I'm having trouble showing a dialog that says "Please wait". I tied also using a Handler, but that didn't work either. What's the best to control the "Please wait" dialog from the 2nd Thread? Thanks!

public void showWaitDialog() {

    prgDialog = new ProgressDialog(context);
    prgDialog.setTitle("Please wait.");
    prgDialog.setMessage("Please wait.");
    prgDialog.setCancelable(false);
    prgDialog.show();


}

You can:

  • Define an Handler in your UI Thread (eg in the Activity ) and then pass it to your thread. Now from the thread you call handler.post(runnable) to enqueue code to be executed on the UIThread.

  • Define a BroadcastReceiver in your Activity and from you thread send an Intent with the necessary information in the Bundle

  • Use an AsyncTask and the methods publishProgress (), onProgressUpdate() or onPostExecute() to inform the Activity of the progress or when the taask has finished

  • Use runOnUiThread .

It depends on your needs. For short-running asynchronous operations, AsyncTask is a good choice.

Why not use an AsyncTask . You can tell the Task on onPreExecute() to show the Please wait dialog, and then onPostExecute(Result result) you can remove the dialog. Those two methods are working on the UI thread while doInBackground(Params... params) is occurring in a background thread.

Example:

private class GetGPSTask extends AsyncTask<null, null, null>{

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
                    showWaitDialog();  <-Show your dialog
    }


    @Override
    protected void doInBackground(null) {

                //your code to get your GPS Data
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
                    HideDialogbox(); <-Code to hide the dialog box
    }
}

Just remember to change the template types if you need to. Where it says AsynTask , the first value is passed to doInBackground , 2nd value is for progress value, 3rd value is a return value from doInBackground to onPostExecute .

As other answers have rightly suggested, you can preferably use AsyncTask . Here is an exampleof how to use it for your purpose: AsyncTask Android example . Otherwise you may use runOnUiThread method as well. From inside your second thread to make the changes on UI thread ( eg: Dialogs and Toasts). According to its documentation , it says:

It runs the specified action on the UI thread. If the current thread is the UI thread, then the action is executed immediately. If the current thread is not the UI thread, the action is posted to the event queue of the UI thread.

For eg;

Your_Activity_Name.this.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            // your stuff to update the UI
            showWaitDialog();

        }
    });

See display progressdialog in non-activity class and Loading Dialog with runOnUiThread for update view on Android . hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM