14

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

4

5 回答 5

9

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

/**
 * 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 的侦听器对其进行更新。

于 2014-08-21T22:52:12.107 回答
6

我最终得出了这个结论。它适用于 S3 和 S4。它应该适用于任何 4+ Android 版本。

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

            if (state) {
                Log.d("HDMIListner", "BroadcastReceiver.onReceive() : Connected HDMI-TV");
                Toast.makeText(ctxt, "HDMI >>", Toast.LENGTH_LONG).show();    
            } else {
                Log.d("HDMIListner", "HDMI >>: Disconnected HDMI-TV");
                Toast.makeText(ctxt, "HDMI DisConnected>>", Toast.LENGTH_LONG).show();
            }
        }
    }
}

AndroidManifest.xml 需要将其放入应用程序标记中:

    <receiver android:name="__com.example.android__.HdmiListener" >
        <intent-filter>
            <action android:name="android.intent.action.HDMI_PLUGGED" />
        </intent-filter>
    </receiver>
于 2014-01-27T14:31:57.383 回答
5

您可以从中获取数据/sys/class/display/display0.hdmi/connect。如果文件内容为0,则表示未连接 HDMI,否则为1,表示已连接 HDMI。

try {
    File file = new File("/sys/class/display/display0.hdmi/connect");
    InputStream in = new FileInputStream(file);
    byte[] re = new byte[32768];
    int read = 0;
    while ((read = in.read(re, 0, 32768)) != -1) {
        String string = new String(re, 0, read);
        Log.v("String_whilecondition", "HDMI state = " + string);
        result = string;
    }
    in.close();
} catch (IOException ex) {
    ex.printStackTrace();
}
于 2014-03-05T07:00:38.730 回答
1

这里同样的问题。一些谷歌告诉我,除了摩托罗拉之外,其他制造商没有太大希望,但是来自http://developer.sonymobile.com/wp/2012/05/29/how-to-use-the-hidden-hdmi-api-教程/

应用程序可以通过监听广播意图来检测设备是否通过 HDMI 连接器连接:“com.sonyericsson.intent.action.HDMI_EVENT”

于 2012-08-10T18:13:27.860 回答
1

检查文件/sys/class/switch/hdmi/state,如果它是 1 则它已连接到 HDMI。

于 2013-08-21T15:25:46.553 回答