[英]android interval between sensor listeners
I wrote a simple android code, that listens to acceleration and orientation sensors, and writes the acceleration x,y,z values, and the angel with north. 我编写了一个简单的android代码,它侦听加速度和方向传感器,并写入加速度x,y,z值和带有north的天使。
In the app i am trying to set a 30 seconds interval between sending sensors event to onSensorChanged() function, but it's still updating a few times every second. 在该应用程序中,我试图将发送传感器事件到onSensorChanged()函数之间的间隔设置为30秒,但它仍每秒更新几次。
this is the code: 这是代码:
package com.example.sensors;
import java.util.List;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
public class MainActivity extends Activity implements SensorEventListener{
private SensorManager sensorManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sensorManager = (SensorManager)this.getSystemService(SENSOR_SERVICE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) //
{
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
Log.i("acceleration:", "x, y, z" + " " + x + " " + y + " " + z + " ");
}
else if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) //@deprecated. use TYPE_MAGNETIC_FIELD
{
Log.i("orentation: " , "north:" + event.values[0]);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
protected void onResume() {
super.onResume();
// register this class as a listener for the accelerometer sensors and orientation
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),//This method get the default sensor for a given type
30000000);//SensorManager.SENSOR_DELAY_NORMAL
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),//This method get the default sensor for a given type
30000000);
}
@Override
protected void onPause() {
// unregister listener
super.onPause();
sensorManager.unregisterListener(this);
}
}
You can't set a custom interval for sensor rates. 您无法为传感器速率设置自定义间隔。 The slowest rate possible is SENSOR_DELAY_UI, and the samples per second depend on the device. 最低速率可能是SENSOR_DELAY_UI,每秒采样数取决于设备。 You will need to filter these values by saving the last time you printed the values, then wait until the difference between that time and the current time is over 30 seconds and print again. 您将需要通过保存上次打印值的时间来过滤这些值,然后等待直到该时间与当前时间之间的时差超过30秒,然后再次打印。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.