如何让传感器定期工作?传感器、工作

2023-09-07 15:37:14 作者:怪我懦弱i

我目前正在开发中的Andr​​oid的应用程序,将记录传感器数据的时间长度固定的几个周期。比如,我打算记录数据10秒,然后停下来,让手机休息10秒,然后重新开始记录,...在此模式中1小时的工作。我的问题是,如何让手机自动执行这个计划吗?我目前使用下面code(从Android:如何收集传感器值的一段固定的时间?),但它仅适用于一个循环,我必须手动启动新的周期后,我相信previous周期已经结束。

 公共无效onResume(){
mSensorManager.registerListener(mListener,mSensorAcceleration,SensorManager.SENSOR_DELAY_GAME);
mSensorManager.registerListener(mListener,mSensorMagnetic,SensorManager.SENSOR_DELAY_GAME);
处理程序H =新的处理程序();
h.postDelayed(新的Runnable(){

    @覆盖
    公共无效的run(){
    //做的东西与传感器值
        mSensorManager.unregisterListener(mListener);
    }
},10000);
 

...

任何帮助将AP preciated !!

解决方案

第1步:将您的活动实施的Runnable ,而不是使用匿名内部类,移动你的的run()方法要在活动的实施。

第二步:在您的的run()的方法,安排自己(的活动)再次使用的延迟后运行 postDelayed()。以 postDelayed(),将有效地建立一个定期调用的run()

第三步:跟踪你是否在和传感器关模式传感器,并在的run(),无论是注册或注销听众合适。

第四步:在的onPause(),呼叫 removeCallbacks()处理器停止定期调用的run()

您会看到这样的日程安排,自己对运行再次的此示例项目。下面是活动:

 包com.commonsware.android.post;

进口android.app.Activity;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Toast;

公共类PostDelayedDemo扩展活动实现Runnable {
  私有静态最终诠释周期= 5000;
  私人查看根= NULL;

  @覆盖
  保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    根= findViewById(android.R.id.content);
  }

  @覆盖
  公共无效onResume(){
    super.onResume();

    跑();
  }

  @覆盖
  公共无效的onPause(){
    root.removeCallbacks(本);

    super.onPause();
  }

  @覆盖
  公共无效的run(){
    Toast.makeText(PostDelayedDemo.this,谁 - 呼!,Toast.LENGTH_SHORT)
         。显示();
    root.postDelayed(本,期限);
  }
}
 
蓝牙振动检测传感器 传感器 苏州捷研芯

I am currently developing an app in Android which will record sensor data for a fixed length of time for several cycles. For example, I plan to record the data for 10 seconds, and then stop, let the phone rest for 10 seconds, and start record again, ... working in this pattern for 1 hour. My question is, how to let the phone automatically execute this plan? I am currently using code below ( from Android: How to collect sensor values for a fixed period of time?) , but it only works for one cycle, I have to manually start new cycles after I am sure the previous cycle has finished.

public void onResume() {
mSensorManager.registerListener(mListener, mSensorAcceleration, SensorManager.SENSOR_DELAY_GAME);
mSensorManager.registerListener(mListener, mSensorMagnetic, SensorManager.SENSOR_DELAY_GAME);
Handler h = new Handler();
h.postDelayed(new Runnable() {

    @Override
    public void run() {
    //    do stuff with sensor values
        mSensorManager.unregisterListener(mListener);               
    }
}, 10000);

...

Any help will be appreciated!!

解决方案

Step #1: Have your activity implement Runnable, rather than use an anonymous inner class, moving your run() method to be implemented on the activity.

Step #2: In your run() method, schedule yourself (the activity) to run again after a delay using postDelayed(). This, plus your existing call to postDelayed(), will effectively set up a periodic call to run().

Step #3: Keep track of whether you are in "sensors on" or "sensors off" mode, and, in run(), either register or unregister the listeners as appropriate.

Step #4: In onPause(), call removeCallbacks() on your Handler to stop the periodic calls to run().

You will see an example of this sort of schedule-yourself-to-run-again logic in this sample project. Here is the activity:

package com.commonsware.android.post;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class PostDelayedDemo extends Activity implements Runnable {
  private static final int PERIOD=5000;
  private View root=null;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    root=findViewById(android.R.id.content);
  }

  @Override
  public void onResume() {
    super.onResume();

    run();
  }

  @Override
  public void onPause() {
    root.removeCallbacks(this);

    super.onPause();
  }

  @Override
  public void run() {
    Toast.makeText(PostDelayedDemo.this, "Who-hoo!", Toast.LENGTH_SHORT)
         .show();
    root.postDelayed(this, PERIOD);
  }
}