2011-12-27 6 views
11

HDMI 장치가 Android 장치에 연결되어 있는지 여부를 감지해야합니다. 이것을 위해 BroadcastReceiver를 사용 중이며이를 감지 할 수도 있습니다. 그러나 BroadcastReceiver를 사용하여 응용 프로그램을 시작하기 전에 HDMI 장치가 연결되어있는 경우 시나리오를 처리 할 수 ​​없습니다. 이 경우 BroadcastReceiver는 HDMI 장치가 연결되어 있는지 여부를 찾을 수 없습니다. HDMI 장치가 연결되어 있는지 여부를 알 수있는 방법이 있습니까?Android에서 HDMI 기기 연결 상태를 확인하는 방법은 무엇인가요?

+0

브로드 캐스트 리시버 코드를 공유하십시오. Motorola 장치에서만 작동하도록 설계되었습니다 ... ?? –

+0

[This] (https://stackoverflow.com/a/21383495/1921481) 대답이 나를 위해 효과가 있었지만 의도를 바꾸기 만하면되었습니다. "android.intent.action.HDMI_PLUGGED"대신 "android.intent.action.HDMI_HW_PLUGGED"여야합니다. (이 답변에 대한 의견이 더 많았 으리라 생각했지만 내 평판으로 인해 댓글을 달 수 없습니다.) –

답변

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/상태, 그것이 HDMI

+0

이 장치가 루팅되어야한다고 가정합니까? – Adi

+0

아니요, 장치가 루팅 될 필요가 없습니다 –

4

에 연결 .sonyericsson.intent.action.HDMI_EVENT는 "나는 결국이 나왔다. S3 및 S4에서 작동합니다. 그것은 4 + 안드로이드 버전과 함께 작동합니다.

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

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

+0

나는 장치를 뿌리 뽑을 필요가 없다고 생각한다. –

관련 문제