Search code examples
androidservicekeyevent

Key Events through a service


I have a service that plays a notification sound. The sound is user defined so it can be anything. If it has a long play time I want the user to be able to stop it by pressing any physical button on the phone. How do I go about setting up my OnKeyListener?


Solution

  • This is not possible in general.

    If the device has a CAMERA or MEDIA button (latter being on wired headsets), and if the foreground activity does not consume the event, then there is a broadcast Intent (ACTION_CAMERA_BUTTON and ACTION_MEDIA_BUTTON, respectively) that you can listen for. However, relatively few devices will have one of these.