BroadcastReceiver 的 Manifest 和 Programmatic 注册之间的主要区别

发布于 2024-09-18 01:21:58 字数 552 浏览 2 评论 0原文

我试图了解在清单中注册 BroadcastReceiver 和以编程方式注册之间的主要区别...

我的理解基本上如下(如果我遗漏了某些内容,希望有人纠正我的观点)。

  • 在清单中注册:

    1. 操作系统会在需要时神奇地找到并实例化您的类,调用 onReceive() 方法,无论您的应用程序的运行状态如何
    2. 您的接收每次广播只会被调用一次(即您可以认为在清单中注册就像注册您的“类”以接收广播 - 并且广播根据需要实例化您的类)(??)
  • 以编程方式注册:

    1. 在代码中注册意味着您正在注册类的实例来接收广播消息(即,如果您的代码有点草率,并且您设法注册了多次,那么您最终将得到多个 BroadcastReceiver 实例,它们都具有 onReceive( )要求广播
    2. 要取消注册,您需要取消注册之前注册的特定 BroadcastReceiver 实例
    3. 如果您的应用程序被操作系统销毁,则不会调用您的 onReceive() 方法来进行广播

谢谢

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

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

  • Registered in Manifest:

    1. The OS will magically find and instantiate your class if needed, calling the onReceive() method, regardless what the running state of your application was
    2. 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) (??)
  • Registered Programmatically:

    1. 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
    2. to unregister, you need to unregister the specific BroadcastReceiver instance that you previously registered
    3. if your application gets destroyed by the OS, your onReceive() method will not be called for a broadcast

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮年 2024-09-25 01:21:58

你说的基本正确。

请注意,清单注册的接收者对象仅使用一次。为每个广播创建一个 BroadcastReceiver 的新实例。清单注册接收器的主要用途是当您的代码不在内存中时可能会继续进行的广播(例如,BOOT_COMPLETED,您通过AlarmManager安排的警报)。

You have it basically correct.

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).

北城孤痞 2024-09-25 01:21:58

何时使用哪种方法注册

使用哪种方法注册 BroadcastReceiver 取决于您的应用对系统事件的处理方式。我认为您的应用程序想要了解系统范围的事件基本上有两个原因:

  1. 您的应用程序围绕这些事件提供某种服务

  2. 您的应用程序希望对状态更改做出适当的反应

第一类的示例是需要在设备启动后立即工作的应用程序,或者每当应用程序启动时都必须开始某种工作的应用程序已安装。 Battery Widget Pro 或 App2SD 是这类应用程序的好例子。对于这种类型,您必须在 Manifest 文件中注册 BroadcastReceiver。

第二类的示例是表明应用程序可能依赖的环境发生变化的事件。假设您的应用程序依赖于已建立的蓝牙连接。您必须对状态变化做出反应——但前提是您的应用程序处于活动状态。在这种情况下,不需要静态注册的广播接收器。动态注册的会更合理。

还有一些活动甚至不允许您静态注册。一个例子是 Intent.ACTION_TIME_TICK 事件,每分钟广播一次。这是一个明智的决定,因为静态接收器会不必要地耗尽电池电量。

When to use which method to register

Which method to use for registering your BroadcastReceiver depends on what your app does with the system event. I think there are basically two reasons why your app wants to know about system-wide events:

  1. Your app offers some kind of service around these events

  2. Your app wants to react graciously to state changes

Examples for the first category are apps that need to work as soon as the device is booted or that must start some kind of work whenever an app is installed. Battery Widget Pro or App2SD are good examples for these kinds of apps. For this type you must register the BroadcastReceiver in the Manifest file.

Examples for the second category are events that signal a change to circumstances your app might rely on. Say your app depends on an established Bluetooth connection. You have to react to a state change – but only when your app is active. In this case there is no need for a statically registered broadcast receiver. A dynamically registered one would be more reasonable.

There are also a few events that you are not even allowed to statically register for. An example for this is the Intent.ACTION_TIME_TICK event which is broadcast every minute. Which is a wise decision because a static receiver would unnecessarily drain the battery.

你是年少的欢喜 2024-09-25 01:21:58

根据我的理解,你的理解是正确的。

另一个相关(且模糊)的区别是,某些特定的系统 Intents 仅在以编程方式注册的情况下才会触发您的接收器。仅在清单中定义的接收者不会被调用。
示例为:ACTION_SCREEN_ON, ACTION_SCREEN_OFFACTION_BATTERY_CHANGEDACTION_HEADSET_PLUG

我推荐这个文本,提及有关意图和接收器的各种详细信息。

Your understanding is correct according to mine.

Another relevant (and obscure) difference is that some specific system Intents will only trigger your receiver if it's programmatically registered. Receivers only defined in the manifest won't be called.
Examples are: ACTION_SCREEN_ON, ACTION_SCREEN_OFF, ACTION_BATTERY_CHANGED, ACTION_HEADSET_PLUG

I'd recommend this text that mentions various details about Intents and Receivers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文