Skip to content Skip to sidebar Skip to footer

Alert Dialog From Thread - Android

I have a thread that sends GPS coordinates to a database every six seconds and I have a check that verifies that the user is within a defined area. If the user is not within the lo

Solution 1:

It is a little difficult to read the entire code, but I will show you how to display an AlertDialog from a background Thread:

Create a handler inside onCreate(), or onResume()... something that runs on the UI-Thread:

...onCreate(){
      //...
      mHandler=newHandler();
}

Then inside your Thread() just use:

mHandler.post(new Runnable() {
    publicvoidrun(){
        //Be sure to pass your Activity class, not the Thread
        AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
        //... setup dialog and show
    }
});

Solution 2:

     runOnUiThread(new Runnable() {
                     publicvoidrun() {

                              //your alert dialog here..

                    }
                });

Solution 3:

new Handler().post(new Runnable{
 publicvoidrun(){

   //create your AlertDialog here.. 
  }

});

see more here

Post a Comment for "Alert Dialog From Thread - Android"