I came out with this eventually. It's working on S3 and S4. It should work with any 4+ Android version.
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 needs this into application tag: