Android capturing volume up/down key presses in broadcast receiver?

后端 未结 4 869
感动是毒
感动是毒 2020-12-06 21:13

I\'m trying to make an application where the user can override the default behaviour of the volume up/down buttons (as well as the screen on/off button - is this possible?).

相关标签:
4条回答
  • 2020-12-06 21:38

    This is not (easily) possible, because the "onKeyDown" method is overriding Activity.onKeyDown(). For this reason, you have to have a foreground activity to receive these function calls.

    I don't have any experience doing this, but you would have to dig a bit deeper into writing your own hardware key intercept/handler functions, since you wouldn't be able to access the one from the activity class.

    0 讨论(0)
  • 2020-12-06 21:43

    This probably won't do what you want, but if you make an IME, the IME can capture the volume +/- buttons, and do whatever it wants, like remap to other button presses. For instance, I have modified a Gingerbread-style IME to turn volume +/- buttons to do page up/down in the Kindle and Overdrive apps.

    0 讨论(0)
  • 2020-12-06 21:56

    this isn't a very good idea. this means every time you press the volume up/down keys you're going to capture it regardless of where you are. what if i want to change my ringer volume? media volume? app that uses the rockers for other uses? it just won't work well and just aggravate people. plus, i'm not sure if its doable.

    0 讨论(0)
  • 2020-12-06 21:58

    as well as the screen on/off button - is this possible?

    Fortunately, no.

    But I would like it to be possible even when the application is not open, hence why I'd like to set up a broadcast receiver or maybe stick something in a service to make this possible.

    This is not possible for the volume buttons.

    For example, AndroSS allows you to override the camera hardware button to take a screenshot.

    That is the camera button. If the foreground activity does not consume a camera button click, that gets turned into a broadcast Intent, which other applications can listen for. This behavior is unique to the camera and media buttons and is not used with any other hardware buttons.

    0 讨论(0)
提交回复
热议问题