连续线程监控硬件接近传感器
我将如何实现一个在整个应用程序中运行的线程。我查看过服务,但我很确定这不是我想要的。即使应用程序关闭,我也不希望线程不断检查。我只是想要一个在应用程序后台运行的线程(即使我在活动之间切换)来持续检查用户是否将手机举到耳边。如果用户这样做,它将执行一个操作。有类似这样的例子吗?
How would I go about implementing a thread that runs throughout the application. I have looked at services but I am pretty sure this isnt what I want. I dont want the thread constantly checking even if the application is closed. I just want a thread running in the background of the application (even if I switch between activities) to continously check to see if the user has raised the phone to his/her ear. If the user does then it will perform an action. Any examples of something like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为此,您不需要线程或服务。请参阅 Android API 文档中的 SensorManager 示例。另外,请参阅下面的示例:
You don't need a thread or service for this. See the example in the Android API documentation for the SensorManager. Also, see below for an example:
这最好作为服务来执行。当您的活动开始时绑定到服务,并在您的活动完成时取消绑定到它。服务应在绑定时开始侦听,并在解除绑定时停止。
This is best performed as a service. bind to the service when your activity starts, and unbind to it when your activity has completed. The service should start listening when it's bound to, and stop when unbound from.