Android 4.4 - 播放默认音乐播放器

时间:2013-11-10 14:06:54

标签: android android-4.4-kitkat

任何人都可以告诉我为什么这不再适用于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);
    }

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

我不知道为什么你认为代码首先是可靠的。不要求任何应用程序响应这些特定的广播对象做任何事情。

除此之外,Android还在继续打击发送这类假系统广播的应用,例如Android 4.4 blocking apps sending ACTION_MEDIA_MOUNTED broadcasts。我会检查LogCat,看看你是否有任何关于你滥用这个广播的警告或任何事情。