连续线程监控硬件接近传感器

发布于 2024-11-15 09:09:59 字数 145 浏览 1 评论 0原文

我将如何实现一个在整个应用程序中运行的线程。我查看过服务,但我很确定这不是我想要的。即使应用程序关闭,我也不希望线程不断检查。我只是想要一个在应用程序后台运行的线程(即使我在活动之间切换)来持续检查用户是否将手机举到耳边。如果用户这样做,它将执行一个操作。有类似这样的例子吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

她比我温柔 2024-11-22 09:09:59

为此,您不需要线程或服务。请参阅 Android API 文档中的 SensorManager 示例。另外,请参阅下面的示例:

 public class SensorActivity extends Activity, implements SensorEventListener {
     private final SensorManager mSensorManager;
     private final Sensor mSensor;

     public SensorActivity() {
         mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
         mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
     }

     protected void onResume() {
         super.onResume();
         mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
     }

     protected void onPause() {
         super.onPause();
         mSensorManager.unregisterListener(this);
     }

     public void onAccuracyChanged(Sensor sensor, int accuracy) {
     }

     public void onSensorChanged(SensorEvent event) {
         // TODO: implement your action here.
     }
 }

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:

 public class SensorActivity extends Activity, implements SensorEventListener {
     private final SensorManager mSensorManager;
     private final Sensor mSensor;

     public SensorActivity() {
         mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
         mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
     }

     protected void onResume() {
         super.onResume();
         mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
     }

     protected void onPause() {
         super.onPause();
         mSensorManager.unregisterListener(this);
     }

     public void onAccuracyChanged(Sensor sensor, int accuracy) {
     }

     public void onSensorChanged(SensorEvent event) {
         // TODO: implement your action here.
     }
 }
戏舞 2024-11-22 09:09:59

这最好作为服务来执行。当您的活动开始时绑定到服务,并在您的活动完成时取消绑定到它。服务应在绑定时开始侦听,并在解除绑定时停止。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文