我是初学的android编程 我的问题是我如何在使用android的传感器中执行占空比,这样我需要在某个ON周期(即3秒)开始时注册Acc传感器并在此3秒内记录数据(无论何种类型的数据)关于传感器状态的改变和在开启周期(3秒)结束时取消注册同一传感器,然后传感器进入休眠周期关闭期间(例如10秒)然后再次重复操作。 / p>
如果可能在android中没有,请帮助我,如果它可能明确告诉我如何做到这一点?
我试着写这段代码但是没有验证我需要的东西!!
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);
}