HDMI デバイスが Android デバイスに接続されているかどうかを検出する必要があります。このために、私は BroadcastReceiver を使用しており、それも検出できます。しかし、BroadcastReceiver を使用すると、アプリケーションが起動される前であっても HDMI デバイスが接続されているシナリオを処理できません。この場合、BroadcastReceiver は HDMI デバイスが接続されているかどうかを検出できません。HDMI デバイスが接続されているかどうかを確認する方法はありますか?
5 に答える
私は他の答えと他の場所からのいくつかを使用してこれを思いつきました:
/**
* 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 のリスナーで更新する必要があります。
私は最終的にこれで出てきました。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>
からデータを取得できます/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();
}
ファイル/sys/class/switch/hdmi/state
を確認して、1 の場合は HDMI に接続されています。
ここでも同じ問題。一部のグーグルは、モトローラ以外の他のメーカーにはあまり希望がないと言っていましたが、http://developer.sonymobile.com/wp/2012/05/29/how-to-use-the-hidden-hdmi-api-からチュートリアル/ :
アプリケーションは、ブロードキャスト インテント「com.sonyericsson.intent.action.HDMI_EVENT」をリッスンすることで、デバイスが HDMI コネクタ経由で接続されているかどうかを検出できます。