I am developing a sort of an easy app in Android Studio and I want to have a code that detects when a button of the headphones is pressed, (so that then elicits and action).
I have been looking up on Internet and searching everywhere, and all scripts that I have found, have not worked.
Either due to problems with the versions of Android or because the code just "gets red" and for reasons I don't know, it just doesn't work. I am really desperate and needy of that code. So I'd be so thankful if someone is able to share one.
For instance, here is a code I was using:
public static class MediaButtonIntentReceiver extends BroadcastReceiver {
// int E=0;
public MediaButtonIntentReceiver() {
super();
}
@Override
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) { //Si la acción capturada en intentaction no equivale a una puñsación de boton entonces sale de el método debido al return
MainActivity.E= 7;
return;
}
KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); //extra key se refiere a el de los auriculares
if (event == null) { //Si event es null, return. Event equivale a extrakeyevent. osea que si no hay extrakey event, return
MainActivity.E= 7; return;
}
int action = event.getAction();
if (action == KeyEvent.ACTION_DOWN) { //Lo que hará cuando sea pulsado
MainActivity.E= MainActivity.E+1;
// do something
}
abortBroadcast();
}
}
With the following in the manifest:
<receiver android:name="OuterClass$MediaButtonIntentReceiver">
<intent-filter android:priority="2147483647">
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
The thing is that, when I run the app, and I press the media button of the headphones, nothing happens with the MediaPlayer of Android (As it should normally happen), and that indicates that, in point of fact, the app is having the priority for these buttons over the mediaPlayer of Android. However, nothing happens in the app either with the presses (when it is supposed to happen something). I have to add too, that when the press is long, the a kind of app of Google is opened. I am not interested in long presses though.
Also I have to mention that from where I extracted this, there was more code apart from the already seen, for instance there were things like:
//AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
//ComponentName mReceiverComponent = new ComponentName(this, MediaButtonIntentReceiver.class);
//mAudioManager.registerMediaButtonEventReceiver(mReceiverComponent);
// mAudioManager.unregisterMediaButtonEventReceiver(mReceiverComponent);
(these lines were discarded by me) Anyway this code did not work because Android studio says that they are obsolete.
I also found:
// BroadcastReceiver MediaButtonIntentReceiver = new MediaButtonIntentReceiver(); // MediaButtonIntentReceiver mMediaButtonReceiver = new MediaButtonIntentReceiver(); // IntentFilter mediaFilter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON); //mediaFilter.setPriority(1000000000); // registerReceiver(mMediaButtonReceiver, mediaFilter);
I put it within the onCreate of the MainActivity. And did not work either.
I found a last code in another question of this forum. Which don't work as some parts of the code appear in red (the callback, the return true(and appears "return outside method")...):
MediaSession audioSession = new MediaSession(getApplicationContext(), "TAG"); audioSession.setCallback(new MediaSession.Callback){
//@Override
public static boolean onMediaButtonEvent(final Intent mediaButtonIntent) {
String intentAction = mediaButtonIntent.getAction();
if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
KeyEvent event = mediaButtonIntent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (event != null) {
stopTimeOfGame_millis = event.getDownTime();
double usersReactionTime = (event.getDownTime() - startTimeOfGame_millis) / 1000.0;
UtilsRG.info("event.getDownTime(): " + usersReactionTime);
double getEventTime = (event.getEventTime() - startTimeOfGame_millis) / 1000.0;
UtilsRG.info("event.getEventTime(): " + getEventTime);
int action = event.getAction();
if (action == KeyEvent.ACTION_DOWN) {
long action_down = android.os.SystemClock.uptimeMillis();
double actionDown = (action_down - startTimeOfGame_millis) / 1000.0;
UtilsRG.info("ACTION_DOWN: " + actionDown);
}
if (action == KeyEvent.ACTION_UP) {
long action_up = android.os.SystemClock.uptimeMillis();
double actionUp = (action_up - startTimeOfGame_millis) / 1000.0;
UtilsRG.info("ACTION_UP: " + actionUp);
}
}
}
return true;
}
};
In this, I also had o add:
PlaybackState state = new PlaybackState.Builder()
.setActions(PlaybackState.ACTION_PLAY_PAUSE)
.setState(PlaybackState.STATE_PLAYING, 0, 0, 0)
.build();
audioSession.setPlaybackState(state);
audioSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS | MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
audioSession.setActive(true);
To be more specific: enter image description here
I am getting crazy with this. Please help. haha. Thank you all in advance!