3

I am developing an Android apps where user is able to find the phone once it is lost and I plan to implement the remote ringing function, I would like to do something like when the alarm is ringing, no matter the volume up or down key is pressed, the volume will still remain loudest while the alarm is ringing... But I have no idea how to achieve this... I just able to detect the event of volume keys pressed... Kindly need help for this.. Thanks...

@Override
public boolean onKeyDown(int keyCode, KeyEvent event){

    if (keyCode == KeyEvent.KEYCODE_VOLUME_UP){
        Toast.makeText(this, "Volume Up", Toast.LENGTH_LONG).show();
        return true;
    }

    if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
        Toast.makeText(this, "Volume Down", Toast.LENGTH_LONG).show();
        return true;
    }

    return super.onKeyDown(keyCode, event);
}
user1782267
  • 313
  • 4
  • 10
  • 20
  • What exactly are you trying to do and where are you having problems? The code you posted looks like it should intercept the volume keys without a problem. Is that code working for you? – Bryan Herbst Nov 08 '12 at 03:36
  • when i press the volume key, the toast is shown up... But what i wanna do is when the user let say click the volume down key, the volume is not decreased but still remain the loudest... – user1782267 Nov 08 '12 at 03:38

1 Answers1

3

Your code works fine for me. I threw it into a basic app, and it successfully intercepted the key presses without changing the volume on my device.

Have you tried overriding onKeyUp as well as onKeyDown?

EDIT: Note that this does not work in Services. You simply cannot intercept hardware volume button presses in services.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
  • No, it does not. A few other people have tried, such as [here](http://stackoverflow.com/questions/9052275/how-to-create-a-service-that-would-be-listening-to-the-volume-button), [here](http://stackoverflow.com/questions/6712601/android-capturing-volume-up-down-key-presses-in-broadcast-receiver), and [here](http://stackoverflow.com/questions/10154118/listen-to-volume-buttons-in-background-service). – Bryan Herbst Nov 08 '12 at 03:54
  • If it is not working in Service, can I just create an Activity for remotely ringing the phone without any user interception? – user1782267 Nov 08 '12 at 03:54
  • 1
    I guess I'm not really seeing how ringing the phone (simply playing an audio file, I assume) is related to the volume buttons. It sounds like your goal is simply to set the volume to max, which you can do with [`MediaPlayer.setVolume()`](http://developer.android.com/reference/android/media/MediaPlayer.html#setVolume%28float,%20float%29) – Bryan Herbst Nov 08 '12 at 04:02
  • I did this already in my coding, but what i mean is when the phone is ringing, if user click the volume key down, he/she can actually decrease the volume, but what i wanna do is make sure even user click the volume key down the volume is still at maximum volume, something like disable the volume keys – user1782267 Nov 08 '12 at 05:10
  • 1
    I suppose you could call `setVolume()` to set it to max every time the volume buttons are pressed, or you could also try launching an Activity, as those can intercept the volume keys just fine. – Bryan Herbst Nov 08 '12 at 17:13