java - Detect when user connects his android device to a TV -
java - Detect when user connects his android device to a TV -
how can observe micro hdmi cable connection in code? in example, when user connects android device tv.
use of these 2 or both depending upon requirement :
/** * checks device switch files see if hdmi device/mhl device plugged * in, returning true if so. */ private boolean ishdmiswitchset() { // file '/sys/devices/virtual/switch/hdmi/state' holds int -- if // it's 1 hdmi device connected. // alternative file check '/sys/class/switch/hdmi/state' // exists instead on devices. file switchfile = new file("/sys/devices/virtual/switch/hdmi/state"); if (!switchfile.exists()) { switchfile = new file("/sys/class/switch/hdmi/state"); } seek { scanner switchfilescanner = new scanner(switchfile); int switchvalue = switchfilescanner.nextint(); switchfilescanner.close(); homecoming switchvalue > 0; } grab (exception e) { homecoming false; } }
and broadcastreceiver
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 == true) { 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(); } } } }
declare receiver in manifest.
<receiver android:name="__com.example.android__.hdmilistener" > <intent-filter> <action android:name="android.intent.action.hdmi_plugged" /> </intent-filter> </receiver>
java android android-5.0-lollipop android-4.4-kitkat
Comments
Post a Comment