Detecting Volume Keystrokes Pressing on Android

Asked

Viewed 668 times

5

I found a palliative solution, which keeps monitoring the change in the volume 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 kind "gambiarra".

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

Thanks a lot!

Updating

Solution types like the one that Gravitybox uses only work if the device is root... Still, I would like something as close as possible:

Modvolumekeyskiptrack.java on Github

1 answer

1

Monitor with some kind of service? I see no other way than to use Alarmmanager to check the volume every x seconds.

However, if you are 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); 
}
  • Yeah, I’m gonna re-edit the question. It would have to be from inside a service.

  • Thanks! But the solution with Alarmmanager could not act as quickly as the solution I already found. It has to be something instantaneous, or as close to it as possible.

  • But you can set Alarmmanager for every 1 second.

  • This type of solution that is always running and checking (known as pooling) ends up spending more CPU and battery than necessary, and the response time is not always satisfactory. In this case, with the solution I found of monitoring the volume via callback, there is pooling, and the response time is almost instantaneous... But still, the user feels for a brief moment (few milliseconds) that there has been a change in volume :( I wanted to avoid this, and try to be notified about the physical pressing of the key, if possible.

  • I believe that the solution within the criteria you determined does not exist, at least not to access without Root.

  • I’m also coming to that conclusion. I’ve already reviewed the internet, unsuccessfully. But there are always hopes ;)

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.