的BroadcastReceiver的清单和计划登记的主要区别 [英] Main difference between Manifest and Programmatic registering of BroadcastReceiver

查看:128
本文介绍了的BroadcastReceiver的清单和计划登记的主要区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解注册一个BroadcastReceiver在清单中,并以编程方式对其进行注册的...

I am trying to understand the main differences between registering a BroadcastReceiver in the Manifest and registering it programmatically...

我的理解是基本如下 - 将AP preciate有人纠正我的观点,如果我失去了一些东西。

My understanding is basically as follows - would appreciate someone correcting my points if I am missing something.

在注册清单: - 操作系统会奇迹般地发现,如果需要实例化类,调用的onReceive()方法,无论你的应用程序的运行状态是 - 您收到只会被调用一次,每次广播(即你可以考虑注册在清单就像你注册类用于接收广播 - 与广播实例化类需要)(??)

Registered in Manifest: - The OS will magically find and instantiate your class if needed, calling the onReceive() method, regardless what the running state of your application was - Your receive will only get called once per broadcast (i.e. You can consider that registering in the manifest is like registering your 'class' for receiving the broadcast - and the broadcast instantiates your class as needed) (??)

注册编程方式: - 登记在code意味着你要注册你的类的实例来接收广播信息(例如,如果你的code是一个有点草率,你设法注册了几次,你最终会与多个BroadcastReceiver的实例的所有有他们的onReceive()呼吁广播 - 取消注册,则需要注销您$ pviously注册p $特定的BroadcastReceiver实例 - 如果你的应用程序时由操作系统被破坏,你的onReceive()方法将不会被要求广播

Registered Programmatically: - registering in code means that you are registering instances of your class to receive broadcast messages (i.e. if your code is a little sloppy, and you manage to register several times, you will end up with multiple BroadcastReceiver instances all having their onReceive() called for a broadcast - to deregister, you need to deregister the specific BroadcastReceiver instance that you previously registered - if your application gets destroyed by the OS, your onReceive() method will not be called for a broadcast

感谢

推荐答案

您有它基本上是正确的。

You have it basically correct.

请注意,一个明显的注册的接收器对象仅使用一次。你的的BroadcastReceiver 为每个广播创造了一个新的实例。主要用途清单注册的接收器是广播,可能会去,而你的code不是在内存中(例如, BOOT_COMPLETED ,通过<$ C您预定的报警器$ C> AlarmManager )。

Note that a manifest-registered receiver object is only used once. A new instance of your BroadcastReceiver is created for each broadcast. The primary use of manifest-registered receivers is for broadcasts that may go on while your code is not in memory (e.g., BOOT_COMPLETED, your scheduled alarms via AlarmManager).

这篇关于的BroadcastReceiver的清单和计划登记的主要区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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