android - 接收长按音量键

标签 android android-intent media broadcast receiver

Google-Music-App (在 OnePlus One,Cyanogen OS 版本 12.1 上) 在长按音量增大硬件键 并长按音量减小键播放上一首歌曲。

是否可以在 BroadcastReceiver 中接收这些操作? 还是操作系统只是将这些长按作为 MEDIA_BUTTON 操作处理? (如果我的两个假设都不对,Google Play 是如何做到这一点的?)

编辑 1:

似乎是带有“MEDIA_NEXT”的 MEDIA_BUTTON 操作。但是为什么我的接收器没有收到 Action ?

String action = intent.getAction();
    if(action.equalsIgnoreCase(Intent.ACTION_MEDIA_BUTTON)) {
            KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
            if (KeyEvent.KEYCODE_MEDIA_PLAY == event.getKeyCode()) {
                //my play code
            }else if (KeyEvent.KEYCODE_MEDIA_NEXT == event.getKeyCode()) {
                //my play next code
            }else if (KeyEvent.KEYCODE_MEDIA_PREVIOUS == event.getKeyCode()) {
            //my play prev code
        }else if (KeyEvent.KEYCODE_MEDIA_PAUSE == event.getKeyCode()) {
            //my pause code
        }
    }

它已在 list 中注册:

<receiver android:name=".receiver.AudioPlayerBroadcastReceiver">
        <intent-filter>
            //other actions (they all work fine)
            <action android:name="android.intent.action.MEDIA_BUTTON" />
        </intent-filter>
    </receiver>

最佳答案

Cyanogen 在长按音量增大时发送一个 MEDIA_NEXT 事件。 (至少 Cyanogemod 11 在我的手机上做到了这一点)

您应该能够轻松地对此进行测试。只需收听 MEDIA_NEXT 并检查您是否通过长按接收到它。

要编辑 1: 参见 BroadcastReceiver for ACTION_MEDIA_BUTTON not working

(另外:如果您有新问题,请打开一个新问题而不是编辑旧问题)

关于android - 接收长按音量键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36226675/

相关文章:

android - 如何在 SVN 中获取下一个修订号

java - 静态变量还是通过 Bundle 传递变量?

android - 后退按钮重新启动 Activity 而不是返回

android - 从 Android 应用程序共享文本时如何修复错误消息 "Please use a valid Web address"?

python - “InMemoryUploadedFile”对象没有属性 'get'

android - LongClick 事件发生得太快。如何增加触发它所需的点击时间?

android - 由于某个应用程序掩盖了权限请求,因此“设置”无法验证您的响应

rest - 如何为 RESTful Web 服务创建自定义媒体类型(应用程序/vnd)?

java - 获取库的路径 - Java

java - 使用 try and catch 请求 Android 运行时权限