我是初学者 android 编程我的问题是我如何使用 android 在传感器中执行占空比,这样我需要在某个 ON 周期(即 3 秒)开始时注册 Acc 传感器并仅在这 3 秒内记录数据(无论数据类型如何)关于传感器状态的变化和准确地在 ON 周期(3 秒)结束时取消注册相同的传感器,然后传感器进入休眠周期的关闭周期(例如 10 秒)然后再次重复该操作。
如果可能的话,请帮助我,如果可能的话,请明确告诉我我该怎么做?
我试图编写此代码,但未验证我需要什么!
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.textView1);
tv2 = (TextView) findViewById(R.id.textView2);
tv3 = (TextView) findViewById(R.id.textView3);
tv4 = (TextView) findViewById(R.id.textView4);
tv5 = (TextView) findViewById(R.id.textView5);
sensorManager= (SensorManager) getSystemService(Context.SENSOR_SERVICE);
eol = System.getProperty("line.separator");
sdCard =getExternalFilesDir(FileName);
file = new File(sdCard,FileName);
csd = new collected_sensor_data();
update_timer = new Timer();
t = new MyTask();
update_timer.scheduleAtFixedRate(t,0,3000);
}
class MyTask extends TimerTask {
@Override
public void run() {
//Called each time when 1000 milliseconds (1 second) (the period parameter)
//We must use this function in order to change the text view text
runOnUiThread(new Runnable() {
@Override
public void run() {
csd.onSensorChanged(event);
}
});
}
}
@Override
public void onResume() {
super.onResume();
startSimulation();
}
@Override
public void onPause() {
super.onPause();
stopSimulation();
}
class collected_sensor_data implements SensorEventListener{
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
x= event.values[0];
y= event.values[1];
z= event.values[2];
sum = x+y+z;
tv1.setText(String.valueOf(sum));
}
}
public void startSimulation(){
sensorManager.registerListener(csd ,sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(csd,sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY), SensorManager.SENSOR_DELAY_NORMAL);
}
public void stopSimulation(){
sensorManager.unregisterListener(csd);
}