Android BroadCastReceiver for volume key up and down

后端 未结 1 890
攒了一身酷
攒了一身酷 2020-11-30 07:58

If a user presses volume key up or down is it possible to detect it in my broadcast receiver? I need the full code.

Here is my Intent filter

IntentFi         


        
相关标签:
1条回答
  • 2020-11-30 08:37

    The intent does not have an EXTRA_KEY_EVENT extra. It does have an extra android.media.EXTRA_VOLUME_STREAM_VALUE which contains the new volume.

    int volume = (Integer)intent.getExtras().get("android.media.EXTRA_VOLUME_STREAM_VALUE");
    

    If you store the old value, you can infer whether volume up or down was pressed.

    0 讨论(0)
提交回复
热议问题