user735646
user735646

Reputation: 177

Android Catch KeyEvent in a service

I need to make an application for Android and one of the requirements is that when I press a key (for example the volumekey) my phone needs to tell me the number of unread messages. Everything works fine: I can tell the unread messages to the user with Text-To-Speech, I have a broadcastreceiver, an activity and a service. In the activity you can start or stop the service and in the service I count the unread messages but now I have to know how to catch a key event on the home screen of the phone when te service is running because when the key is pressed, I need to tell the user how many messages are unread. In the activity it is simple to catch a keyevent, but it must also work when the service is running and you're not in the activity. Maybe I can do it with the broadcast receiver but I don't know how.

Upvotes: 0

Views: 1674

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006554

In the activity it is simple to catch a keyevent, but it must also work when the service is running and you're not in the activity.

This is not supported. The closest you can get is ACTION_CAMERA_BUTTON and ACTION_MEDIA_BUTTON, but not all users have those buttons, and they will only be broadcast if the user presses the button and the foreground activity does not consume the event (e.g., camera app using the CAMERA button as a shutter trigger).

Upvotes: 1

Related Questions