ffttyy
ffttyy

Reputation: 864

How to disable to turn off audio sound by clicking up-down buttons on the side of a phone?

I want to start an alarm for some case. Once alarm starts, turning off the alarm with using up-down buttons must be blocked. I find some code but it doesn't work.

AudioManager am =(AudioManager) ctxt.getSystemService(ctxt.AUDIO_SERVICE);
        am.setStreamVolume(
                AudioManager.STREAM_MUSIC,
                am.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
                0);
        am.setStreamMute(AudioManager.STREAM_MUSIC, false);

Edit: Only setStremMute part dosen't work and there is no error.

Upvotes: 0

Views: 256

Answers (1)

fhsilva
fhsilva

Reputation: 1257

You can capture the volume up and down key events in your activity:

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    int keyCode = event.getKeyCode();
        switch (keyCode) {
        case KeyEvent.KEYCODE_VOLUME_UP:
            // Ignore
            return true;
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            // Ignore
            return true;
        default:
            return super.dispatchKeyEvent(event);
        }
    }

Upvotes: 1

Related Questions