我想在Android应用程序中检测是否连接了HDMI线缆。 我找到了一种方法:
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;
}
}
现在的问题是,如果连接了HDMI,我想要做一些事情,但如果布尔值已经翻转,我不想每秒都运行一次威胁检查。还有更好的方法吗?