如何检查Android设备中的HDMI设备连接状态? [英] How to check the HDMI device connection status in Android?

查看:4927
本文介绍了如何检查Android设备中的HDMI设备连接状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检测HDMI设备是否已连接到我的Android设备。
对于这个我使用一个BroadcastReceiver,它也能够检测。
但是使用BroadcastReceiver时,即使在我的应用程序启动之前,我也无法处理HDMI设备连接的情况。在这种情况下,BroadcastReceiver无法找到是否连接了任何HDMI设备。

I need to detect whether an HDMI device is connected or not to my Android device. For this I'm using a BroadcastReceiver and it is able to detect also. But with BroadcastReceiver I'm unable to handle the scenario when the HDMI device is connected even before my application was launced. In this case the BroadcastReceiver is unable to find if any HDMI device is connected or not. Is there any way I can get to know if any HDMI device is connected or not at any point?

推荐答案

有没有什么办法可以知道是否有HDMI设备在任何时候连接?我想出了这个使用其他答案和一些从其他地方:

I came up with this using the other answers and some from elsewhere:

/**
 * Checks device switch files to see if an HDMI device/MHL device is plugged in, returning true if so.
 */
private boolean isHdmiSwitchSet() {

    // The file '/sys/devices/virtual/switch/hdmi/state' holds an int -- if it's 1 then an HDMI device is connected.
    // An alternative file to check is '/sys/class/switch/hdmi/state' which exists instead on certain devices.
    File switchFile = new File("/sys/devices/virtual/switch/hdmi/state");
    if (!switchFile.exists()) {
        switchFile = new File("/sys/class/switch/hdmi/state");
    }
    try {
        Scanner switchFileScanner = new Scanner(switchFile);
        int switchValue = switchFileScanner.nextInt();
        switchFileScanner.close();
        return switchValue > 0;
    } catch (Exception e) {
        return false;
    }
}

如果经常检查,以存储结果并用@ hamen的监听器更新。

If you're checking often, you'd want to store the result and update it with @hamen's listener.

这篇关于如何检查Android设备中的HDMI设备连接状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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