Detecting volume keystrokes on Android

5

I found a palliative solution that monitors the volume change of the system itself. When the volume decreases, for example from 10 to 9, the program resets the volume back to 10 (pretending there was no change in volume) and performs the desired action (idem for when the volume increases).

This solution works on several devices, but I find this solution a bit "gambiarra".

Does anyone know a mode, which does not involve root or custom ROMS, to monitor and handle physical volume keystrokes with the screen locked from a service?

Thanks everyone!

Update

Solution types like the one GravityBox uses only work if the device is root. However, I would like something as similar as possible:

ModVolumeKeySkipTrack.java on GitHub

    
asked by anonymous 24.04.2014 / 17:22

1 answer

1

Monitor with a kind of service? I do not see any way other than using AlarmManager to check every x seconds if there was a change in volume.

However, if it is in Activity, you can use this solution:

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN||) {
        // Sua ação aqui
        return true;
    }
    return super.onKeyDown(keyCode, event); 
}
    
24.04.2014 / 18:44