AsyncTask的在广播接收器在Android为一个死线程发送错误信息到处理程序 [英] AsyncTask in BroadcastReceiver in Android gives sending message to a Handler on a dead thread error

查看:124
本文介绍了AsyncTask的在广播接收器在Android为一个死线程发送错误信息到处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在机应用来实现推送通知。一旦收到推送消息称我与的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:


  1. 新的通知推到设备上。

  2. 您收到通知您广播接收机。

  3. 您火IntentService兑WS
  4. 执行同步
  5. 的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.

这篇关于AsyncTask的在广播接收器在Android为一个死线程发送错误信息到处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆