在服务中获取触摸事件(坐标、MotionEvents 等)
如何像您一样使用 public boolean onTouchEvent(MotionEvent event)
从 Activity 获取触摸事件?显然,服务中没有可重写的 onTouchEvent
方法,那么我该如何执行此操作呢?有没有直接的方法或可靠的解决方法?
我的想法是创建一个服务,在后台监听用户做出某个手势,然后在用户做出正确的手势时执行某些操作。谢谢你!
编辑:这个问题旨在回答这个问题。
How do I get touch events like you can from an Activity using public boolean onTouchEvent(MotionEvent event)
? There obviously isn't an onTouchEvent
method to override in a service, so how can I go about doing this? Is there a direct way or a reliable work-around?
My idea is to create a service that listens in the background for the user to make a certain gesture, then do something when that user has made the correct gesture. Thank you!
EDIT: This question is intended for the answer to this question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建一个 0x0 视图并将其作为始终位于顶部的系统对话框添加到服务的窗口中。
您可以使用
'WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH'
权限监听事件。不幸的是,您只能获取初始触摸事件,而无法将该事件传递给较低的活动。我很高兴你实际上不能这样做;您能想象恶意软件如何利用此漏洞来获取用户的凭据吗?You can create a 0x0 view and add it to the Service's window as a system dialogue that is always on top.
You can listen to events using the
'WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH'
permission. Unfortunately, you can only get the initial touch event and you can not pass on the event to lower activities. I am glad you can not do that actually; can you imagine how malware would exploit this to obtain user's credentials.