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

标签 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);
        }
    }
}

list 中有这个:
<receiver android:name="[package].HdmiListener" >
    <intent-filter>
        <action android:name="android.intent.action.HDMI_PLUGGED" />
    </intent-filter>
</receiver>

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

我对 AOSP 和其他项目进行了一些挖掘,并能够提出一个更完整的 list 条目版本:
    <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>

但是 还是不行 :-(

所以,我的问题是 - 有没有人让它真正起作用 ?还是正如权限所暗示的那样,它是仅限系统级的功能?

最佳答案

android.intent.action.HDMI_PLUGGEDFLAG_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 来注册。 .

关于android - 在 Android TV 上收听 HDMI 插入和拔出事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50871176/

相关文章:

android - 如何将 EditText inputType 指定为数字和密码?

带有通知徽章的 Android Tablayout 选项卡,例如 WhatsApp

android - 一段时间后服务停止工作。需要连续工作

android - 从 Firebase onMessageReceived 打开 fragment

android - 处理 android 7 Nougat 上的网络变化

java - NDK 支持不同的 Product Flavor

java - 安卓问题 : Textview onchange event

android - 如何在android中的BroadCast接收器的Onreceive中获取全局数据

android.net.conn.CONNECTIVITY_CHANGE 问题

android - 动态注册广播接收器不起作用 - BluetoothDevice.ACTION_FOUND