How to create toast from IntentService? It gets stuck on the screen

后端 未结 3 930
野趣味
野趣味 2020-12-09 02:34

I\'m trying to have my IntentService show a Toast message, but when sending it from the onHandleIntent message, the toast shows but gets stuck and the screen and never leave

3条回答
  •  庸人自扰
    2020-12-09 03:14

    in onCreate() initialize a Handler and then post to it from your thread.

    private class DisplayToast implements Runnable{
      String mText;
    
      public DisplayToast(String text){
        mText = text;
      }
    
      public void run(){
         Toast.makeText(mContext, mText, Toast.LENGTH_SHORT).show();
      }
    }
    protected void onHandleIntent(Intent intent){
        ...
      mHandler.post(new DisplayToast("did something")); 
    }
    

提交回复
热议问题