Android 在服务中捕获 KeyEvent
我需要为Android制作一个应用程序,其中一个要求是当我按下某个键(例如音量键)时,我的手机需要告诉我未读消息的数量。一切正常:我可以通过文本转语音告诉用户未读消息,我有一个广播接收器、一个活动和一个服务。在活动中,您可以启动或停止服务,在服务中,我计算未读消息,但现在我必须知道如何在服务运行时捕获手机主屏幕上的按键事件,因为当按下按键时,我需要告诉用户有多少消息未读。在活动中捕获按键事件很简单,但当服务正在运行且您不在活动中时,它也必须起作用。也许我可以用广播接收器来做到这一点,但我不知道如何做。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不支持此操作。您可以获得的最接近的是
ACTION_CAMERA_BUTTON
和ACTION_MEDIA_BUTTON
,但并非所有用户都有这些按钮,并且只有当用户按下按钮并且前台活动没有按下按钮时,它们才会被广播消耗事件(例如,相机应用程序使用相机按钮作为快门触发器)。This is not supported. The closest you can get is
ACTION_CAMERA_BUTTON
andACTION_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).