何时注销在onCreate中注册的广播接收器? [英] When To Unregister Broadcast Receiver registered in onCreate?

查看:99
本文介绍了何时注销在onCreate中注册的广播接收器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android中,我已经在我的onCreate()中注册了BroadcastReceiver。我应该在哪里注销,这样我就不会泄漏接收者?

In Android, I've registered a BroadcastReceiver in my onCreate(). Where should I unregister so that I won't have leaked receivers?

推荐答案

http://developer.android.com/reference/android/content/BroadcastReceiver.html

您可以通过Context.registerReceiver()动态注册此类的实例,也可以通过AndroidManifest.xml中的标记静态发布实现。注意:如果在Activity.onResume中注册接收者()实作中,您应该在Activity.onPause()中取消注册。(暂停时不会收到意图,这将减少不必要的系统开销。请不要在Activity.onSaveInstanceState()中取消注册。

"You can either dynamically register an instance of this class with Context.registerReceiver() or statically publish an implementation through the tag in your AndroidManifest.xml. Note: If registering a receiver in your Activity.onResume() implementation, you should unregister it in Activity.onPause(). (You won't receive intents when paused, and this will cut down on unnecessary system overhead). Do not unregister in Activity.onSaveInstanceState()."

:) SDK是您最好的朋友。我会说做它在SDK中所说的,除非您在暂停时绝对需要接收器,但要小心。您是否必须动态注册接收器,或者将其放入AndroidManifest.xml中会更好?如果将接收器放入清单中,则无需担心注册/注销。

:) The SDK is your best friend. I would say do what it says in the SDK unless you absolutely need the receiver when paused but be careful. Do you have to dynamically register the receiver or would putting it in the AndroidManifest.xml be better? If you put the receiver in the manifest you won't need to worry about registering/unregistering it.

这篇关于何时注销在onCreate中注册的广播接收器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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