简体   繁体   English

在清单中注册时需要注销Android Broadcastreceiver吗?

[英]Android Broadcastreceiver unregister needed when registered in manifest?

So mainly that is the question, when I register a Broadcastreceiver dynamically, it is needed to be unregistered at onDestroy . 所以主要是这个问题,当我动态注册Broadcastreceiver ,需要在onDestroy上取消注册。 But when the Broadcastreceiver is registered within the manifest, do we need to unregister it? 但是,当在清单中注册了Broadcastreceiver时,是否需要注销它?

You cannot unregister a manifest-registered receiver. 您不能注销清单注册的接收者。 If the receiver is enabled, it will always be listening for broadcasts. 如果启用了接收器,它将始终在收听广播。

You should be careful while adding broadcast receiver because unnecessary broadcast receivers drain battery power. 添加广播接收器时应小心,因为不必要的broadcast receivers消耗电池电量。

If you add the broadcast receiver in the Android manifest file , it's implied that you are going to handle a particular intent in the broadcast receiver and not ignore it. 如果将广播接收器添加到Android清单文件中 ,则意味着您将在广播接收器中处理特定的意图,而不是忽略它。

You can use the PackageManager to enable/disable a BroadcastReceiver in declared in the Manifest. 您可以使用PackageManager启用/禁用清单中声明的BroadcastReceiver The Broadcast Receiver will get fired only when it is enabled. Broadcast Receiver只有在启用后才会被触发。

For more info see Android - how to unregister a receiver created in the manifest? 有关更多信息,请参阅Android-如何注销清单中创建的接收者?

There is a way to enable and disable the broadcast receiver which is added in the manifest file. 有一种方法可以启用和禁用清单文件中添加的广播接收器。

See this post Android broadcast receiver: Registering/unregistering during runtime 参见这篇文章Android广播接收器:运行时注册/注销

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM