如何拦截服务中按下的硬键?
是否有办法拦截通过使用服务按下硬键来启动活动?
换句话说:是否可以在服务中处理 KeyEvent?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了当前活动的应用程序之外,没有通用的方法可以监听硬件按键的按键事件。
通过其广播
Intent
,只能检测到 CAMERA 按钮事件。ACTION_CAMERA_BUTTON 在按下 CAMERA 按钮时广播,并且不会被前台 Activity 拦截: developer.android.com/reference/android/content/…
这意味着只有在前台应用程序未处理按键时,您才能检测到按键。
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.
是的。您可以使用 InputMethodService 捕获 KeyEvent。硬键有自己的键码,就像软键一样。
看看谷歌制作的 SoftKeypad 示例。
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.