Alert Dialog from Thread - Android

后端 未结 3 1721
孤街浪徒
孤街浪徒 2021-01-03 16:23

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

相关标签:
3条回答
  • 2021-01-03 17:00

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

    Then inside your Thread() just use:

    mHandler.post(new Runnable() {
        public void run(){
            //Be sure to pass your Activity class, not the Thread
            AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
            //... setup dialog and show
        }
    });
    
    0 讨论(0)
  • 2021-01-03 17:06
    new Handler().post(new Runnable{
     public void run(){
    
       //create your AlertDialog here.. 
      }
    
    });
    

    see more here

    0 讨论(0)
  • 2021-01-03 17:10
         runOnUiThread(new Runnable() {
                         public void run() {
    
                                  //your alert dialog here..
    
                        }
                    });
    
    0 讨论(0)
提交回复
热议问题