清单与活动中的广播接收器注册 [英] Broadcast Receiver Register in Manifest vs. Activity
问题描述
我需要一些帮助来了解我的广播接收器何时可以在清单中注册而不是从正在运行的活动或服务中注册时可以工作.
I need some help to understand when I can expect my broadcast receiver will work when just registered in the manifest versus having to be registered from a running activity or service.
例如,如果我使用以下意图过滤器注册一个独立的接收器,它可以在没有服务/活动引用的情况下工作:
So for example if I register a stand alone receiver with the following intent filter it works without having a service/activity reference to it:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.blk_burn.standalonereceiver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<receiver android:name="TestReceiver">
<intent-filter>
<action android:name="android.media.AUDIO_BECOMING_NOISY"/>
</intent-filter>
</receiver>
</application>
</manifest>
但是,如果我将 android.media.AUDIO_BECOMING_NOISY
替换为 android.intent.action.HEADSET_PLUG
,则不会触发接收器(Android 文档)
However if I replace android.media.AUDIO_BECOMING_NOISY
with android.intent.action.HEADSET_PLUG
the receiver is not triggered (Android Documentation)
根据我在本网站上的发现,您必须从已运行的活动或服务中注册此接收器才能使其正常工作(发布).
From what I found on this site you have to register this receiver from an activity or service that is already running for it to work (Post).
谁能告诉我为什么仅在清单中调整您的意图过滤器时这不起作用,以及为什么您需要在后台运行引用/注册接收器的服务?
Can anyone tell me why this does not work when just adjusting your intent filter in the manifest and why you need to have a service running in the background that references/registers the receiver?
是否有解决办法,以便我可以使用带有 android.intent.action.HEADSET_PLUG
的 Intent 过滤器在我的应用清单中注册我的接收器?
Is there a work around so that I can just register my receiver in my app's manifest using an intent filter with android.intent.action.HEADSET_PLUG
?
如何从 android 中识别哪些广播操作文档需要让服务或活动注册它们,而不是仅仅在清单中使用正确的过滤器?
How can do I identify which Broadcast actions from the android documentation need to have a service or activity register them versus just having the right filter in the manifest?
推荐答案
如果您的接收器已在清单中注册,并且您的应用程序未运行,则会创建一个新进程来处理广播.如果你在代码中注册它,它与你注册它的活动/服务的生命周期相关.对于某些广播,如果它不存在,创建一个新的应用程序进程实际上没有意义,或者有一些安全、性能等方面的影响,因此您只能在代码中注册接收器.
If your receiver is registered in the manifest, and your app is not running, a new process will be created to handle the broadcast. If you register it in code, it's tied to the life of the activity/service you registered it in. For some broadcasts, it doesn't really make sense to create a new app process if it doesn't exist, or there are some security, performance, etc. implications, and thus you can only register the receiver in code.
至于 HEADSET_PLUG
广播,这个想法似乎是你已经在运行的应用程序可以得到它来对 UI、音量等进行应用程序特定的调整.如果你的应用程序没有运行,你不应该真正关心耳机是否被拔掉.
As for the HEADSET_PLUG
broadcast, it seems the idea is that your already running app can get this to do app-specific adjustments to UI, volume, etc. If your app is not running, you shouldn't really care about the headphones being unplugged.
AFAIK,没有一个地方为所有广播总结了这个信息,但是每个 Intent 都应该在 JavaDoc 中有一个关于如何注册和使用它的评论,但显然它缺乏地方.如果您为 Intent 搜索 Android 源代码树,您应该能够编译一个列表.FLAG_RECEIVER_REGISTERED_ONLY.
AFAIK, there is no single place this info is summarized for all broadcasts, but each Intent should have a comment in the JavaDoc about how to register and use it, but apparently it's lacking in places. You should be able to compile a list if you grep the Android source tree for Intent.FLAG_RECEIVER_REGISTERED_ONLY.
这篇关于清单与活动中的广播接收器注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!