The simple question is how to receive intent from headset button? I have searched for a while and nothing worked out. My app aims on API 15-25
in Manifest
<activity
android:name=".Home"
android:windowSoftInputMode="stateAlwaysHidden"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action
android:name="com.example.action.PAUSE_PLAY_INTENT_FILTER" />
//this should be unique string as action
<action
android:name="com.example.action.NEXT_TRACK_INTENT_FILTER" />
//this should be unique string as action
<action
android:name="com.example.action.PREVIOUS_TRACK_INTENT_FILTER"
/>
//this should be unique string as action
<action android:name="android.media.VOLUME_CHANGED_ACTION" />
<action
android:name="com.example.action.UPDATE_ALL_LISTS_IN_APP" />
//this should be unique string as action/>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</activity>
in onCreate in Activity
IntentFilter mediaIntent = new IntentFilter();
mediaIntent.addAction(Intent.ACTION_MEDIA_BUTTON);
mediaIntent.setPriority(999999999);
registerReceiver(mMemoryStorage.mBroadcastReceiver, mediaIntent);
and in onReceive in BroadcastReceiver
String intentAction = intent.getAction();
if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)){
KeyEvent event = (KeyEvent) intent
.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
int keycode = event.getKeyCode();
int action = event.getAction();
Log.i("keycode", String.valueOf(keycode));
Log.i("action", String.valueOf(action));
//onKeyDown(keyCode, event)
if (keycode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE
|| keycode == KeyEvent.KEYCODE_HEADSETHOOK&&action == KeyEvent.ACTION_DOWN) {
if(mMemoryStorage!=null){
System.out.println("playpause");
if(mMemoryStorage.musicSrv.isPng()){
mMemoryStorage.musicSrv.pausePlayer();
mMemoryStorage.musicSrv.updateNotification();
} else if(mMemoryStorage.musicSrv.isPaused){
mMemoryStorage.musicSrv.playPlayer();
mMemoryStorage.musicSrv.updateNotification();
}
//update seekbar playback in PlaySongFragment
Message message = new Message();
message.what = Params.UPDATE_SEEKBAR_PLAYSONGFRAGMENT;
mMemoryStorage.mHandler.sendMessage(message);
}
}