Detect if HDMI is connected via event

僤鯓⒐⒋嵵緔 提交于 2019-12-11 04:24:02

问题


I want to detect in my Android application if a HDMI cable is connected. I found a way how to do that:

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;
        }


}

The problem now ist that I want to do something if HDMI is connected but I dont want to run a threat checking every second if the boolean has flipped. Is there a better way?

来源:https://stackoverflow.com/questions/40264579/detect-if-hdmi-is-connected-via-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!