如果要注销广播接收器? [英] When To Unregister Broadcast Receiver?

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

问题描述

在Android的,我注册一个BroadcastReceiver在我的onCreate()。我应该在哪里注销,这样我就不会泄露接收者?

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

感谢你。

推荐答案

<一个href="http://developer.android.com/reference/android/content/BroadcastReceiver.html">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.

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

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