Celem jest przechwycenie transmisji z zestawu słuchawkowego, a także ostatecznie bluetooth, aby reagować na różne typy kliknięć z zestawu słuchawkowego, aby zmienić odtwarzacz multimediów. To rozwiązanie działa dobrze we wszystkich wersjach przed ICS. Oto niektóre z kodu i rzeczy próbowałem:MediaButtonIntentReceiver nie działa w systemie Android 4.0+
....
private BroadcastReceiver mediaButtonReceiver = new MediaButtonIntentReceiver();
....
public void onCreate() {
...
IntentFilter mediaFilter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
mediaFilter.setPriority(2147483647); // this is bad...I know
this.registerReceiver(mediaButtonReceiver, mediaFilter);
...
}
public class MediaButtonIntentReceiver extends BroadcastReceiver {
private KeyEvent event;
public MediaButtonIntentReceiver() {
super();
}
@Override
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
return;
}
event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (event == null) {
return;
}
try {
int action = event.getAction();
switch(action) {
case KeyEvent.ACTION_UP :
Log.d("TEST", "BUTTON UP");
break;
case KeyEvent.ACTION_DOWN :
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE :
Log.d("TEST", "BUTTON DOWN");
break;
}
} catch (Exception e) {
Log.d("TEST", "THIS IS NOT GOOD");
}
abortBroadcast();
}
}
spróbować do tej pracy, to brzmi jak wersji 4.0 lub nowszej wymaga czegoś takiego, który zrobił praca:
((AudioManager)getSystemService(AUDIO_SERVICE)).registerMediaButtonEventReceiver(new ComponentName(this, MediaButtonIntentReceiver.class));
Próbowałem nawet dodaj to do manifestu, oprócz powyższego:
<receiver android:name=".MediaButtonIntentReceiver" android:enabled="true">
<intent-filter android:priority="2147483647" >
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
Nadal nie ma szczęścia. Czego tu mi brakuje? Z pewnością jest to problem 4.0 +/ICS/JellyBean ... Robi się to w usłudze, a nie w działaniu.
Też znalazłem to jako przyczynę w rdzeniu Androida, ale nie było sposobu na obejście tego: http://code.google.com/p/media-button-router/issues/detail?id=10#c4 – Du3