roh*_*itb 13 connection android hdmi
我需要检测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;
}
}
Run Code Online (Sandbox Code Playgroud)
如果您经常进行检查,则需要存储结果并使用@ 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();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
AndroidManifest.xml需要将其转换为应用程序标记:
<receiver android:name="__com.example.android__.HdmiListener" >
<intent-filter>
<action android:name="android.intent.action.HDMI_PLUGGED" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
小智 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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15932 次 |
| 最近记录: |