1

This code doesn't work will screen locked. what should I do if I want volume key work while screen locked?

My code is :

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    int action = event.getAction();
    int keyCode = event.getKeyCode();
        switch (keyCode) {
        case KeyEvent.KEYCODE_VOLUME_UP:
            if (action == KeyEvent.ACTION_UP) {
                //TODO
            }
            return true;
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            if (action == KeyEvent.ACTION_DOWN) {
                //TODO
            }
            return true;
        default:
            return super.dispatchKeyEvent(event);
        }
    }
Kara
  • 6,115
  • 16
  • 50
  • 57
Reza Shek
  • 581
  • 1
  • 8
  • 18

2 Answers2

3

you can register BroadcastReceiver with action "android.media.VOLUME_CHANGED_ACTION" :

android.media.VOLUME_CHANGED_ACTION

Other way of doing is: volume key on Android .

Community
  • 1
  • 1
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • my code actually work fine but the problem is, it doesn't work while screen locked. what I have to do? if it's possible please give me an example. thank you. – Reza Shek Apr 27 '12 at 12:57
  • it's not working. I created a BroadCastReceiver and it's working fine while the phone is not locked, but as soon as phone get lock then I can't capture if the key pressed. do you have any idea? – Reza Shek Apr 26 '15 at 19:43
  • @mrsh did you find a solution for that? I am facing the same problem – Bruno Siqueira Mar 17 '16 at 17:28
  • Unfortunately, no. if you find anything please keep me posted. Thank you. – Reza Shek Mar 17 '16 at 19:59
3

Do this in a service:

public class MyService extends Service {

@Override
public void onCreate() {
    super.onCreate();
    final BroadcastReceiver vReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
             //your code here
        }
    };
    registerReceiver(vReceiver, new IntentFilter("android.media.VOLUME_CHANGED_ACTION"));
}

}

Then register BroadcastReceiver with action Intent.ACTION_SCREEN_OFF to play a silent sound continuously when screen is off and action Intent.ACTION_SCREEN_ON to stop music when screen on. Volume buttons are only active when there's music playing.

k8C
  • 414
  • 4
  • 9
  • Thank you so much, how can I play silent sound continuously? – Reza Shek Jun 28 '16 at 13:09
  • You can use SoundPool, MediaPlayer or ToneGenerator. With the two formers, download a blank music file from here http://www.xamuel.com/blank-mp3s/ ,use SoundPool.play(load, 0, 0, 0, -1, 1) or MediaPlayer.setLooping(true) and .setVolume(0,0) to play silent and continuous sound. With ToneGenerator, use ToneGenerator.startTone(ToneGenerator.TONE_CDMA_DIAL_TONE_LITE). https://developer.android.com/reference/android/media/SoundPool.html https://developer.android.com/reference/android/media/MediaPlayer.html https://developer.android.com/reference/android/media/ToneGenerator.html – k8C Jun 28 '16 at 17:28