我正在使用计步器,我想存储前一天采取的步骤,并在第二天重置步数,同时仍然将之前的数据存储在另一个textView
中。
这是我的代码:
public class StepsActivity extends AppCompatActivity implements SensorEventListener {
SensorManager sensorManager;
TextView textView;
boolean running=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_steps);
textView = (TextView)findViewById(R.id.tv_steps);
sensorManager =(SensorManager)getSystemService(Context.SENSOR_SERVICE);
}
@Override
protected void onResume() {
super.onResume();
running = true;
Sensor countSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
if (countSensor != null) {
sensorManager.registerListener(this, countSensor, SensorManager.SENSOR_DELAY_UI);
} else {
Toast.makeText(this,"Sensor not found", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onPause() {
super.onPause();
running = false;
sensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
if (running) {
textView.setText(String.valueOf(sensorEvent.values[0]));
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
}