如何从服务显示吐司?发送消息到处理程序上的死线 [英] How to display Toast from a Service? Sending message to a Handler on a dead thread
问题描述
更新:我不同意这是一个重复的 - 因为我在寻找一种方式来退出主应用程序,仍然从服务显示吐司
UPDATE: I don't agree that this is a duplicate - because I am seeking for a way to exit the main app and still show a Toast from the service.
在<一个href=\"https://github.com/afarber/android-newbie/tree/q12/MyNotification/src/de/afarber/mynotification\"相对=nofollow>一个非常简单的测试应用我有2个按钮:
点击任何按钮,将运行一个相应的操作字符串即服务(打开或闪) -
Clicking any of the buttons will run a service with a corresponding action string ("open" or "flash") -
<一个href=\"https://github.com/afarber/android-newbie/blob/q12/MyNotification/src/de/afarber/mynotification/OpenActivity.java\"相对=nofollow> OpenActivity.java :
public class OpenActivity extends Activity {
private Intent mServiceIntent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_open);
mServiceIntent = new Intent(this, RegionService.class);
}
public void openCar(View v) {
mServiceIntent.setAction("open");
startService(mServiceIntent);
}
<一个href=\"https://github.com/afarber/android-newbie/blob/q12/MyNotification/src/de/afarber/mynotification/RegionService.java\"相对=nofollow> RegionService.java :
public class RegionService extends IntentService {
private static final String TAG = "RegionService";
@Override
protected void onHandleIntent(Intent intent) {
Log.d(TAG, "Received an intent: " + intent);
String action = intent.getAction();
Log.d(TAG, "Received an action: " + action);
if(action.equals("open")) {
Toast.makeText(this,
getString(R.string.car_opened),
Toast.LENGTH_SHORT).show();
}
不幸的是我的应用程序崩溃,
Unfortunately my app crashes with:
D/RegionService(24506): Received an intent: Intent { act=open cmp=de.afarber.mynotification/.RegionService }
D/RegionService(24506): Received an action: open
W/MessageQueue(24506): Handler (android.os.Handler) {422768a8} sending message to a Handler on a dead thread
W/MessageQueue(24506): java.lang.RuntimeException: Handler (android.os.Handler) {422768a8} sending message to a Handler on a dead thread
W/MessageQueue(24506): at android.os.MessageQueue.enqueueMessage(MessageQueue.java:320)
W/MessageQueue(24506): at android.os.Handler.enqueueMessage(Handler.java:626)
W/MessageQueue(24506): at android.os.Handler.sendMessageAtTime(Handler.java:595)
W/MessageQueue(24506): at android.os.Handler.sendMessageDelayed(Handler.java:566)
W/MessageQueue(24506): at android.os.Handler.post(Handler.java:326)
W/MessageQueue(24506): at android.widget.Toast$TN.hide(Toast.java:370)
W/MessageQueue(24506): at android.app.ITransientNotification$Stub.onTransact(ITransientNotification.java:54)
W/MessageQueue(24506): at android.os.Binder.execTransact(Binder.java:412)
W/MessageQueue(24506): at dalvik.system.NativeStart.run(Native Method)
作为一个Android编程的新手,我不知道如何显示从服务以正确的方式吐司?
Being an Android programming newbie I wonder How to display a Toast from Service in a correct way?
我想我已经在Android主看到祝酒词(即有设备屏幕上没有任何活动,仍然有祝酒词)。
I think I've already seen Toasts at Android Home (i.e. there was no Activity on the device screen and still there were Toasts).
我的背景:我想监视从我服务的灯塔设备,并显示一些文字祝酒词 - 即使我的应用程序已经关闭
My background: I would like to monitor a beacon device from my service and show some text Toasts - even when my app has been closed.
推荐答案
OnHandleIntent
将在歧线程中运行
所以你展示吐司
中未在android系统允许一个线程
OnHandleIntent
will run in a differant Thread
so you are showing Toast
in a thread which is not allowed in android
因此改变你的code这样的
so change your code like this
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
getString(R.string.car_opened),
Toast.LENGTH_SHORT).show();
}
});
从这个<一个href=\"http://stackoverflow.com/questions/3296639/toast-created-in-an-intentservice-never-goes-away/16766109#16766109\">dead线程服务
IntentService将创建一个线程来处理新的意图,立即终止它一旦任务完成。因此,面包会出1死线程控制。
IntentService will create a thread to handle the new intent, and terminated it immediately once the task has done. So, the Toast will be out of controlled by a dead thread.
您应该在控制台中看到一些例外的时候敬酒显示在屏幕上。
You should see some exceptions in the console when the toast showing on the screen.
这篇关于如何从服务显示吐司?发送消息到处理程序上的死线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!