广播接收器注册的清单与活动 [英] Broadcast Receiver Register in Manifest vs. Activity

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

问题描述

我需要一些帮助理解的时候,我可以预料,当刚注册的清单与具有从正在运行的活动或服务注册我的广播接收器将工作。

因此​​,举例来说,如果我注册一个独立的接收器有以下意图过滤器它的工作原理,无需服务/活动参考吧:

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.blk_burn.standalonereceiver
    安卓版code =1
    机器人:VERSIONNAME =1.0>

    <使用-SDK安卓的minSdkVersion =10/>
    <使用-权限的Andr​​oid:名称=android.permission.WAKE_LOCK/>

    <应用
        机器人:图标=@可绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME>

        <接收器的Andr​​oid版本:NAME =TestReceiver>
            <意向滤光器>
                <作用机器人:名称=android.media.AUDIO_BECOMING_NOISY/>
            &所述; /意图滤光器>
        < /接收器>

    < /用途>

< /舱单>
 

但是,如果我把 android.media.AUDIO_BECOMING_NOISY android.intent.action.HEADSET_PLUG 的接收器不触发(<一href="http://developer.android.com/reference/android/content/Intent.html#ACTION_HEADSET_PLUG">Android文档)

这是我发现在这个网站,你必须注册这个接收器从活动或服务已在运行,它的工作(<一href="http://stackoverflow.com/questions/4202046/which-permission-required-in-order-to-get-action-headset-plug-inside-broadcast-r/4444069#comment14172404_4444069">Post).

  • 谁能告诉我,为什么在刚调整的意图过滤器的清单,这并不工作,以及为什么你需要有在引用/注册接收后台运行的服务?

  • 有没有一种解决办法,这样使用一个意图过滤器 android.intent.action.HEADSET_PLUG 我可以只登记自己的接收器在我的应用程序的清单<? / P>

  • 该怎么办我确定从广播行动<一href="http://developer.android.com/reference/android/content/Intent.html#FLAG_INCLUDE_STOPPED_PACKAGES">android文档需要有一个服务或活动进行注册与仅仅有正确的过滤器清单?

解决方案

如果您接收方是注册在清单和您的应用程序没有运行,一个新的进程会创建来处理广播。如果您在code寄存器它,它依赖于你在注册时的活动/服务的生命。对于一些广播,它并没有真正意义的创建一个新的应用程序,如果它不存在,或存在一定的安全,性能等方面的影响,这样的话你只能登记在code接收器。

对于 HEADSET_PLUG 播出,看来这个想法是,你已经在运行的应用程序可以得到这个程序做特定调整UI,音量等,如果您应用程序没有运行,你不应该真正关心的耳机被拔掉。

AFAIK,没有一个地方这个信息概括为所有广播,但每个意图应在关于如何注册和使用它的JavaDoc注释,但显然它缺乏在的地方。您应该能够编写一个清单,如果你的grep Android的源代码树的<一个href="http://developer.android.com/reference/android/content/Intent.html#FLAG_RECEIVER_REGISTERED_ONLY">Intent.FLAG_RECEIVER_REGISTERED_ONLY.

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>

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?

  • 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?

  • 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 you 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.

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, there is no one 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 at places. You should be able to compile a list if you grep the Android source tree for Intent.FLAG_RECEIVER_REGISTERED_ONLY.

这篇关于广播接收器注册的清单与活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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