1

谁能告诉我为什么这不再适用于 Kitkat 以及如何解决它?

    private Intent player;
    player = new Intent(Intent.ACTION_MEDIA_BUTTON);
    synchronized (this) {
        player.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY));
        sendOrderedBroadcast(player, null);

        player.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY));
        sendOrderedBroadcast(player, null);
    }
4

2 回答 2

3

API 级别 19 中有专门为此目的创建的新方法 AudioManager.dispatchMediaKeyEvent(KeyEvent)。

http://developer.android.com/reference/android/media/AudioManager.html#dispatchMediaKeyEvent(android.view.KeyEvent)

此代码适用于 KitKat:

AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);

long eventtime = SystemClock.uptimeMillis() - 1;
KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0);
am.dispatchMediaKeyEvent(downEvent);

eventtime++;
KeyEvent upEvent = new KeyEvent(eventtime,eventtime,KeyEvent.ACTION_UP,KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0);         
am.dispatchMediaKeyEvent(upEvent);
于 2013-12-07T09:36:00.723 回答
0

我不知道为什么您认为该代码首先是可靠的。不要求任何应用程序对这些特定的广播进行任何响应。

除此之外,Android 还在继续打击发送此类虚假系统广播的应用程序,例如Android 4.4 阻止应用程序发送ACTION_MEDIA_MOUNTED广播。我会检查 LogCat,看看您是否有任何警告或任何关于您滥用此广播的内容。

于 2013-11-10T14:22:46.583 回答