Android 服务中的关键事件
我做了一些研究,得出的结论是我无法使用 Android 服务来捕获关键事件。但是 - 也许有些人会有任何解决方法。我有一个播放器应用程序,我想使用耳机上的特殊按钮(我的三星 Galaxy S 附带)来播放/暂停该应用程序。这是用于接听和结束电话线上设置的电话的按钮。 我测试过这个按钮 - 它等于 KeyCode.KEYCODE_BUTTON_B
const (79)
。当我的播放器在屏幕上时可以处理它,但我也想在手机锁定(屏幕关闭)时播放/暂停。您认为这可能吗? 对于呼叫接收,我认为 android 使用了一个技巧:当有人呼叫时,屏幕会变为活动状态,并且呼叫接收活动可以处理关键事件(我认为 - 这就是它的工作方式,但我可能是错的)。 我不知道 - 如何使用此按钮播放/暂停我的应用程序。
I've made some research concluded that afaik I can't have an Android service for key events capture. But - maybe some will have any workaround. I have a player application, which i want to play/pause using a special button on my earphones (included for my samsung galaxy S). This is button for reciving and ending phone calls set on phones cable.
Ive tested this button - it equals KeyCode.KEYCODE_BUTTON_B
const (79)
. And it can be handled when my player is on screen, but I'd like to play/pause also when my phone is locked (with screen off). Do You think this is possible?
For call reciving I think android uses a trick: when someone calling, the screen goes active, and the call-reciving activity can handle key events (I think - this is how it's work, but I can be wrong).
I have no idea - how to play/pause my app using this button.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为没有什么真正好的方法可以从应用程序级别实现您想要做的事情。然而,我可以想出的一种可能的解决方案是:
监听屏幕关闭和打开的意图。当您收到 SCREEN_OFF 消息时,启动一个“空白”活动,该活动除了监听您的按钮按下并将其传递到您的音乐服务外,什么也不做。
当您收到 SCREEN_ON finish() 这个空白活动时。
我不确定这种方法是否有效,但我使用了类似的方法来在屏幕关闭时监听音量按钮的按下情况。
请注意监听 SCREEN_OFF 和 SCREEN_ON 意图。当我这样做时,我必须在 java 代码中而不是在清单中为接收器设置过滤器。由于某种原因,当我从清单中设置意图过滤器时,它没有正确接收这些意图。
I don't think there is any real good way to achieve what you want to do from the application level. However one possible solution I can come up with is this:
Listen for screen off and on Intents. When you receive SCREEN_OFF start up a "blank" activity that does nothing but listen for your button press and passes it along to your music service.
When you recieve SCREEN_ON finish() this blank activity.
I don't know for sure that this approach will work, but I used something similar to be able to listen for volume button presses while the screen was off.
Note about listening for SCREEN_OFF and SCREEN_ON intents. When I did this I had to set the filter for my receiver in java code rather than in the manifest. For some reason when I set the intent filter from the manifest it wasn't receiving those intents properly.