如何从IntentService创建吐司?它卡在屏幕上

我正在尝试让我的IntentService显示Toast消息, 但是当从onHandleIntent消息发送它时,toast显示但是卡住了屏幕并且从不离开。 我猜它是因为onHandleIntent方法不会发生在主服务线程上,但是我怎么能移动呢? 有人有这个问题并解决了吗?     
已邀请:
onCreate()
初始化一个
Handler
然后从你的帖子发布到它。
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")); 
}
    
以下是完整的IntentService类代码,演示了帮助我的Toasts:
package mypackage;

import android.app.IntentService;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

public class MyService extends IntentService {
    public MyService() { super("MyService"); }

    public void showToast(String message) {
        final String msg = message;
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            }
        });
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        showToast("MyService is handling intent.");
    }
}
    
使用Handle发布一个Runnable,其中包含您的操作内容
protected void onHandleIntent(Intent intent){
    Handler handler=new Handler(Looper.getMainLooper());
    handler.post(new Runnable(){
    public void run(){ 
        //your operation...
        Toast.makeText(getApplicationContext(), "hello world", Toast.LENGTH_SHORT).show();
    }  
}); 
    

要回复问题请先登录注册