在Android TV上收听HDMI插入和拔出的事件

时间:2018-06-15 07:44:27

标签: android broadcastreceiver android-broadcastreceiver android-tv

我需要接收Android TV应用的HDMI状态事件。

我在几个地方找到了这段代码:

public class HdmiListener extends BroadcastReceiver {

    private static String HDMIINTENT = "android.intent.action.HDMI_PLUGGED";

    @Override
    public void onReceive(Context ctxt, Intent receivedIt) {
        String action = receivedIt.getAction();

        if (action.equals(HDMIINTENT)) {
            boolean state = receivedIt.getBooleanExtra("state", false);
        }
    }
}

在清单中显示:

<receiver android:name="[package].HdmiListener" >
    <intent-filter>
        <action android:name="android.intent.action.HDMI_PLUGGED" />
    </intent-filter>
</receiver>

不起作用。广播接收器永远不会被触发。

我通过AOSP和其他项目进行了一些挖掘,并且能够提供更完整版本的清单条目:

    <permission android:name="android.permission.TV_INPUT_HARDWARE"
        android:protectionLevel="signatureOrSystem"
        tools:ignore="SignatureOrSystemPermissions" />

    <permission android:name="android.permission.CAPTURE_TV_INPUT"
        android:protectionLevel="signatureOrSystem"
        tools:ignore="SignatureOrSystemPermissions" />

    <receiver android:name="[package].HdmiListener"
        android:enabled="true"
        android:exported="true" >
        <intent-filter>
            <action android:name="android.intent.action.HDMI_PLUG" />
            <action android:name="android.intent.action.HDMI_PLUGGED" />
            <action android:name="android.intent.action.TVOUT_PLUG" />
            <action android:name="android.media.action.HDMI_AUDIO_PLUG" />
        </intent-filter>
    </receiver>

仍无效: - (

所以,我的问题是 - 是否有人让这个实际工作?或者它是一个仅限系统级的功能,如权限所暗示的那样?

1 个答案:

答案 0 :(得分:0)

android.intent.action.HDMI_PLUGGED具有FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT标志。从Intent.java中的注释中:

 FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT

 * If set, when sending a broadcast before boot has completed only
 * registered receivers will be called -- no BroadcastReceiver components
 * will be launched.  Sticky intent state will be recorded properly even
 * if no receivers wind up being called.  If {@link #FLAG_RECEIVER_REGISTERED_ONLY}
 * is specified in the broadcast intent, this flag is unnecessary.
 *
 * <p>This flag is only for use by system sevices as a convenience to
 * avoid having to implement a more complex mechanism around detection
 * of boot completion.

至少,应该通过调用registerReceiver来注册接收者。