How to intercept a hard key pressed in a service?

坚强是说给别人听的谎言 提交于 2019-12-19 10:27:37

问题


Is there anyway to intercept that a hard key was pressed by using a service in order to launch an activity?

In other words : Is it possible to handle the KeyEvents in the Service?


回答1:


There is no general way to listen for the key events of the hardware keys from anything besides the currently active application.

Only the CAMERA button event can be detected, via its broadcast Intent.

The ACTION_CAMERA_BUTTON is broadcast when the CAMERA button is pressed and is not intercepted by the foreground activity: developer.android.com/reference/android/content/…

This means you can only detect the press if the application in the foreground is not handling the key press.




回答2:


Yes. You can capture KeyEvents with an InputMethodService. The Hard-Keys have their own key codes, just like the Soft-Keys.

Look at the SoftKeypad example that google made.



来源:https://stackoverflow.com/questions/3356325/how-to-intercept-a-hard-key-pressed-in-a-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!