gpt4 book ai didi

android - 如何在Android中查看HDMI设备连接状态?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:22:27 28 4
gpt4 key购买 nike

我需要检测 HDMI 设备是否连接到我的 Android 设备。为此,我使用了 BroadcastReceiver,它也能够检测到。但是对于 BroadcastReceiver,即使在我的应用程序启动之前连接 HDMI 设备时,我也无法处理这种情况。在这种情况下,BroadcastReceiver 无法找到是否连接了任何 HDMI 设备。有什么方法可以随时了解是否连接了任何 HDMI 设备?

最佳答案

我使用其他答案和其他地方的一些答案想出了这个:

/**
* 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 的监听器更新它。

关于android - 如何在Android中查看HDMI设备连接状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8644665/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com