Monitors the connection status of HDMI devices.
The most recent development of TV boxes involves tedious device status listening. Therefore, you can record the connection status listening method of HDMI for later viewing.
There are two methods:
(1) Common broadcast listeners
Register a dynamic broadcast to obtain the plug-in and plug-in of the HDMI interface. Its Action name is "android. intent. action. HDMI_PLUGGED". The specific code is as follows:
private static BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent receivedIt) {
String action = receivedIt.getAction();
if (action.equals("android.intent.action.HDMI_PLUGGED")) {
boolean state = receivedIt.getBooleanExtra("state", false);
if (state) {
isHdmiConnect = true;
} else {
isHdmiConnect = false;
}
}
}
};
In this way, you can listen to the plug-and-play of the HDMI interface, but there is a problem that when you first enter the program, you do not know the status of the HDMI and need to cooperate with the second method.
(2) read the content in the System File
Directly read the data in the system file. The path is "/sys/devices/virtual/switch/hdmi/state". The specific code for changing the storage path of some devices is as follows:
Private static 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 {
Optional switchfilelayout = new partition (switchFile );
Int switchValue = switchfile=. nextInt ();
Switchfiletings. close ();
Return switchValue> 0;
} Catch (Exception e ){
Return false;
}
}
This method can directly obtain the status. However, if the status is frequently queried for multiple times, an exception may be thrown. We recommend that you use the two methods in the most stable way.