onCreate方法内部
sensorManager =
(SensorManager) getSystemService(SENSOR_SERVICE);
注册步进检测器
stepCountSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);
sensorEventListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
Log.d(TAG, "onSensorChanged: called ");
注册步骤并将其输入到textview
if(sensorEvent.sensor.getType() == Sensor.TYPE_STEP_DETECTOR){
Log.d(TAG, "onSensorChanged: STEP DETECTED!");
Toast.makeText(getApplicationContext(),"Step detected", Toast.LENGTH_SHORT).show();
stepCountET.setText("Steps :" + step++);
}else if(sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
Log.d(TAG, "onSensorChanged: ACCELEROMETER! = " + sensorEvent.values);
}
}
检查加速度计或步数计数器
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
Log.d(TAG, "onAccuracyChanged: called");
}
};
sensorManager.registerListener(sensorEventListener, stepCountSensor, SensorManager.SENSOR_DELAY_NORMAL);
}