Skip to content

Instantly share code, notes, and snippets.

@oleynikd
Created November 7, 2017 18:38
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save oleynikd/1065521ee3236a60b804ad063020021f to your computer and use it in GitHub Desktop.
Save oleynikd/1065521ee3236a60b804ad063020021f to your computer and use it in GitHub Desktop.
Android TV HDMI plug/unplug event example
package ua.youtv.testapp;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import static android.media.AudioManager.ACTION_HDMI_AUDIO_PLUG;
import static android.media.AudioManager.EXTRA_AUDIO_PLUG_STATE;
/*
* MainActivity class that loads {@link MainFragment}.
*/
public class MainActivity extends Activity {
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(eventReceiver);
}
@Override
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_HDMI_AUDIO_PLUG);
registerReceiver(eventReceiver, filter);
}
private BroadcastReceiver eventReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// pause video
String action = intent.getAction();
switch (action) {
case ACTION_HDMI_AUDIO_PLUG :
// EXTRA_AUDIO_PLUG_STATE: 0 - UNPLUG, 1 - PLUG
Log.d("MainActivity", "ACTION_HDMI_AUDIO_PLUG " + intent.getIntExtra(EXTRA_AUDIO_PLUG_STATE, -1));
break;
}
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment