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>

这是详细 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;
                    }
                }
            }
        }
    }
}

我尝试无法工作

4

2 回答 2

0

我使用 mMediaButtonReceiverComponent = new ComponentName(this, MusicIntentReceiver.class); mAudioManager.registerMediaButtonEventReceiver(mMediaButtonReceiverComponent);

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

无法控制android 4.4.2的遥控器

于 2016-11-13T07:33:55.557 回答
0

如果您希望您的应用在屏幕关闭时继续工作,您需要获取PARTIAL_WAKE_LOCK

小心,这些按钮在屏幕关闭时不起作用是有原因的,这是为了节省电池。您必须非常小心您的应用程序不会耗尽手机电池。

于 2016-11-12T09:42:56.510 回答