2011-12-27 153 views
11

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

+0

分享您的广播接收器代码。是否仅为摩托罗拉设备工作??? – 2012-04-03 09:32:00

+0

[This](https://stackoverflow.com/a/21383495/1921481)回答为我工作,只需要替换意图。而不是“android.intent.action.HDMI_PLUGGED”它必须是“android.intent.action.HDMI_HW_PLUGGED”。 (这应该是更多的评论,但我的声誉不允许我发表评论)。 – 2017-12-21 00:24:00

回答

6

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

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

0

检查文件/ SYS /班/开关/ HDMI /状态,如果是1,那么它连接到HDMI

+0

这是否假设设备必须被植入? – Adi 2014-03-12 11:01:18

+0

不,设备不需要根植 – 2014-03-14 22:19:24

4

我想出了这个最终。它正在开发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> 
+2

这帮助我检测hdmi是否连接或断开连接,但不知道是在运行应用程序之前连接了hdmi。 – jch 2014-07-25 21:40:22

4

您可以从/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(); 
} 
+0

这是否假设该设备必须是植根的? – Adi 2014-03-12 11:00:53

+0

我认为没有必要植根设备。 – 2014-03-31 08:00:23