【问题标题】:Static broadcast not working ANDROID静态广播不起作用ANDROID
【发布时间】:2015-07-02 20:26:25
【问题描述】:

这是我的接收文件

package com.example.tony.headsetplug;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class MusicIntentReceiver extends BroadcastReceiver {

    @Override public void onReceive(Context context, Intent intent) {
        Log.i("Tag","got receiver");
    }
}

我正在尝试实现用于插入耳机的静态广播,我的清单文件中定义了静态广播。

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <receiver
        android:name=".MusicIntentReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.headset_plug" />
        </intent-filter>
    </receiver>

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>





</application>

它不起作用,甚至没有显示收到的广播。

【问题讨论】:

    标签: android static broadcastreceiver android-broadcast


    【解决方案1】:

    ACTION_HEADSET_PLUG 的值为"android.intent.action.HEADSET_PLUG"(注意其中的大写字母)。检查documentation

    p.s.:此外,您必须以编程方式注册接收器(例如检查 this post),例如:

    IntentFilter receiverFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
    HeadsetStateReceiver receiver = new HeadsetStateReceiver();
    registerReceiver( receiver, receiverFilter );
    

    【讨论】:

    • 谢谢,当它在主活动或任何活动中注册时,它在活动运行时工作。即使活动关闭,我仍然希望接收广播。那可能吗?或者为此,我应该在后台运行服务吗?
    • 是的,在这种情况下使用服务(启动它STICKY,参见developer.android.com/reference/android/app/…)。我忘了提及:在onDestroy() 中使用unregisterReceiver(...),因为两次注册BroadcastReceiver 会导致异常。 p.s.:使用 LocalBroadcastManager 可以有一些优势(参见 developer.android.com/reference/android/support/v4/content/…);此处示例:intertech.com/Blog/…
    • 谢谢,这很有帮助。
    • 如果您能对我的问题投票或接受它,如果您认为这是最有用的答案,我会很高兴;)
    【解决方案2】:

    来自AudioManager.ACTION_HEADSET_PLUG上的官方API文档:

    广播动作:有线耳机插入或拔出。 您无法通过清单中声明的​​组件接收此信息,只能通过 Context.registerReceiver() 显式注册。

    意图将具有以下额外值:

    • 状态 - 0 表示未插入,1 表示已插入。
    • 名称 - 耳机类型,人类可读的字符串
    • 麦克风 - 如果耳机有麦克风,则为 1,否则为 0

    这应该解释为什么它在定义为静态时不能正常工作。我认为由于音乐应用程序通常依赖于这种广播,因此当用户实际上并没有在听音乐或做任何与音频相关的事情时,接收这样的广播是没有意义的。

    编辑:

    如果您想在插入耳机时启动您的音乐应用,但您的 Activity 没有运行,该怎么办?

    考虑使用后台服务,除了注册处理 ACTION_HEADSET_PLUG 的接收器之外,它并没有做太多的事情。为了在设备(重新)启动时启动服务,您可以添加一个静态接收器来监听 ACTION_BOOT_COMPLETED,它会启动服务。

    顺便说一句:如果您想知道是否可以在安装应用后立即启动服务,但您不能。至少根据这个post

    希望这会有所帮助。

    【讨论】:

    • 但是,如果我想在插入耳机时自动播放音乐,但音乐应用程序没有运行(或活动)
    【解决方案3】:

    你必须通过代码注册接收器,这是必须的:

    stackoverflow.com/questions/6249023/detecting-whether-a-headset-is-plugged-into-an-android-device-or-not

    如果你想让你的监听器一直打开,你应该监听系统启动意图并在那个监听器中注册。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-14
      • 1970-01-01
      • 2017-10-12
      相关资源
      最近更新 更多