AsyncTask的在广播接收器在Android为一个死线程发送错误信息到处理程序接收器、错误信息、线程、程序

2023-09-09 21:00:04 作者:再見,再也不賤。

我在机应用来实现推送通知。一旦收到推送消息称我与的AsyncTask的帮助下web服务。当正在使用的应用程序或者是内存这工作得很好。但是,如果我停止从设置的应用程序,然后如果邮件推送到设备,有一种情况例外,如对死线程发送消息到一个处理程序。这可能是由于应用程序不活。

I have implemented Push Notification in my appliction. Once the push message is received I call a webservice with the help of AsyncTask. This works fine when the application is being used or is in the memory. But if I stop the application from settings and then if the message is pushed to the device, there is an exception such as "sending message to a Handler on a dead thread". This is probably since the app is not live.

有人可以让我知道如果它好吗实际上从推送通知接收器取出的AsyncTask,只是处理消息?

Can someone let me know if its alright to actually remove AsyncTask from push notification receiver and just handle the message?

问候苏尼尔

推荐答案

IMO的AsyncTask只应作为相关的活动(即UI)。后台任务的助手

IMO AsyncTask should be only used as a helper for background tasks related to Activities (that is, UI).

在你的情况我会用一个IntentService,它是一种服务,是ppared执行后台任务$ P $。所以:

In your case I would use an IntentService, it is a Service that is prepared to execute background task. So:

新的通知推到设备上。您收到通知您广播接收机。您火IntentService兑WS 执行同步的IntentService做了这个工作,完成时它提出了一个广播事件。

在你的活动附上广播接收机广播在步骤4这样,如果它是present,别的什么也没有发生你的用户界面得到通知。您也可以选择在你的清单注册一个默认的广播接收器处理的步骤4的广播,例如,显示状态栏通知。

In your Activity attach a broadcast receiver for the broadcast in step 4. This way your UI gets notified if it's present, else nothing bad happens. Optionally you can register a default broadcast receiver in your Manifest to handle the broadcast of step 4 and, for example, display a status bar notification.