本文介绍了如何从IntentService创建吐司?它卡在屏幕上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的IntentService显示一条Toast消息,但是,当从onHandleIntent消息发送时,吐司面包会显示但会卡住,而且屏幕也不会消失.我猜测是因为onHandleIntent方法未在主服务线程上发生,但是如何移动它?

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 leaved.I'm guessing its because the onHandleIntent method does not happen on the main service thread, but how can I move it?

有人遇到这个问题并解决了吗?

Has anyone has this issue and solved it?

推荐答案

初始化Handler,然后从您的线程发布到它.

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"));
}

这篇关于如何从IntentService创建吐司?它卡在屏幕上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 22:17