从IntentService registerReceiver不打广播接收器 [英] registerReceiver from IntentService doesn't hit BroadcastReceiver
问题描述
我创建了一个 IntentService
下载使用Android的下载管理器的文件。以下是我的类别:
I've created an IntentService
for downloading a file using Android's DownloadManager. Following is my Class:
public class DownloadService extends IntentService {
public DownloadService() {
super("name");
}
BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
function();
}
};
@Override
protected void onHandleIntent(Intent intent) {
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
Utility.download(someURL);
}
}
我在清单此项:
<service
android:name=".services.DownloadService"
android:enabled="true" >
</service>
在广播接收器的code(函数())
即使文件被下载永远不会执行。我也试过在服务做同样的事情
而不是 IntentService
的,它的工作。
如何让广播接收器里面工作IntentService?
The code inside BroadcastReceiver(function())
is never executing even if the file is downloaded. I also tried doing the same thing in Service
instead of IntentService
, it worked.
How can I make BroadcastReceiver work inside IntentService?
推荐答案
避免调用 registerReceiver
使用IntentService的上下文方法。
Avoid calling registerReceiver
method using IntentService's context.
一旦你的<一个href=\"http://developer.android.com/reference/android/app/IntentService.html#onHandleIntent(android.content.Intent)\"相对=nofollow> IntentService#onHandleIntent 方法返回时,的服务#stopSelf(INT)方法将被调用。因为泄露所有还未注销接收器将予以考虑。该框架甚至警告您:
Once your IntentService#onHandleIntent method returns, the Service#stopSelf(int) method will be called. All the not-yet-unregistered receivers will be considered as leaked. The framework even warns you about that:
E/ActivityThread﹕ Service com.your.packagename.YourIntentService has leaked IntentReceiver com.your.packagename.YourReceiver@41f4cd08 that was originally registered here. Are you missing a call to unregisterReceiver()?
android.app.IntentReceiverLeaked: Service com.your.packagename.YourIntentService has leaked IntentReceiver com.your.packagename.YourReceiver@41f4cd08 that was originally registered here. Are you missing a call to unregisterReceiver()?
at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:796)
at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:597)
at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1438)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1418)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1412)
at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:467)
at com.your.packagename.YourIntentService.onHandleIntent(YourIntentService.java:79)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.os.HandlerThread.run(HandlerThread.java:61)
当然,你没有泄露广播接收机的事后会收到通知。
And of course, none of your leaked broadcast receivers will receive notifications afterwards.
如果你仍然想注册内的广播接收器的 IntentService#onHandleIntent 的方法,使用(例如)为应用的上下文。
If you still want to register the broadcast receiver within IntentService#onHandleIntent method, use (for example) Application's context for that.
getApplication().registerReceiver(receiverInstance,
new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
不过请注意,您必须添加,一旦你与它完成了注销每个这样的接收器实例的逻辑。
Note however, that you'll have to add logic that unregisters each such receiver instance once you finish with it.
这篇关于从IntentService registerReceiver不打广播接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!