0
<receiver android:name=".MusicIntentReceiver" android:exported="false">
            <intent-filter>
                <action android:name="android.media.AUDIO_BECOMING_NOISY" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.MEDIA_BUTTON" />
            </intent-filter>
        </receiver>

This is detail MusicIntentReceiver ... public class MusicIntentReceiver extends BroadcastReceiver { private static final String TAG = LogHelper.makeLogTag(MusicIntentReceiver.class);

    @Override
    public void onReceive(Context context, Intent intent) {
        //LogHelper.i(TAG, "-------------------------------- MusicIntentReceiver.");
        if (intent.getAction().equals(android.media.AudioManager.ACTION_AUDIO_BECOMING_NOISY)) {
            LogHelper.d(TAG, "Headphones disconnected.");
            // send an intent to our MusicService to telling it to pause the audio
            try {
                //TODO Something
            }
            catch (Exception e) {
                //LogHelper.i(TAG, "MusicIntentReceiver onReceive ", e.getMessage());
            }
        }
        else {
            if (!MainActivity.getInstance().isSDK50orGreater()) {
                if (intent.getAction().equals(Intent.ACTION_MEDIA_BUTTON)) {
                    KeyEvent keyEvent = (KeyEvent) intent.getExtras().get(Intent.EXTRA_KEY_EVENT);
                    if (keyEvent.getAction() != KeyEvent.ACTION_DOWN)
                        return;
                    //LogHelper.i(TAG, "---------------------------- onReceive ", keyEvent.getKeyCode());
                    switch (keyEvent.getKeyCode()) {
                        case KeyEvent.KEYCODE_HEADSETHOOK:
                        case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                            context.startService(new Intent(MediaContant.ACTION_TOGGLE_PLAYBACK));
                            break;
                        case KeyEvent.KEYCODE_MEDIA_PLAY:
                            context.startService(new Intent(MediaContant.ACTION_PLAY));
                            break;
                        case KeyEvent.KEYCODE_MEDIA_PAUSE:
                            context.startService(new Intent(MediaContant.ACTION_PAUSE));
                            break;
                        case KeyEvent.KEYCODE_MEDIA_STOP:
                            context.startService(new Intent(MediaContant.ACTION_STOP));
                            break;
                        case KeyEvent.KEYCODE_MEDIA_NEXT:
                            context.startService(new Intent(MediaContant.ACTION_NEXT));
                            break;
                        case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                            // previous song
                            context.startService(new Intent(MediaContant.ACTION_PREVIOUS));
                            break;
                    }
                }
            }
        }
    }
}

I trying cannot working

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
NPBA
  • 129
  • 1
  • 2
  • 9
  • Which part is not working? Can you insert log statements or insert some toasts and tell us which ones are not being activated? Are you getting errors in your logcat? What phone are you testing this on? Is it Android 5.0 or greater? – Stephan Branczyk Nov 12 '16 at 09:30
  • When screen off not working – NPBA Nov 12 '16 at 09:31
  • Possible duplicate of [Capture media button on Android >=4.0 (works on 2.3)](http://stackoverflow.com/questions/10537184/capture-media-button-on-android-4-0-works-on-2-3) – Yaroslav Mytkalyk Nov 12 '16 at 11:41

2 Answers2

0

If you want your app to keep on working when the screen is off, you'll need to acquire a PARTIAL_WAKE_LOCK

Be careful, there is a reason those buttons don't work when the screen is off, it's to conserve battery. You'll have to be extremely careful your application doesn't drain the phone battery.

Stephan Branczyk
  • 9,363
  • 2
  • 33
  • 49
  • I believe the CPU is up and running while playing music. However you might want to release the wake lock after ~5-10 minutes of silence. – Eugen Pechanec Nov 12 '16 at 09:52
0

I use mMediaButtonReceiverComponent = new ComponentName(this, MusicIntentReceiver.class); mAudioManager.registerMediaButtonEventReceiver(mMediaButtonReceiverComponent);

Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); mediaButtonIntent .setComponent(mMediaButtonReceiverComponent); mRemoteControlClientCompat = new RemoteControlClient(PendingIntent.getBroadcast(getApplicationContext() /context/, 0 /requestCode, ignored/, mediaButtonIntent /intent/, 0 /flags/)); mAudioManager.registerRemoteControlClient(mRemoteControlClientCompat);

Cannot control remote control for android 4.4.2

NPBA
  • 129
  • 1
  • 2
  • 9