package com.example.bushu_chuanganqi;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;public classMainActivity extends Activity {privateTextView mStepTV;privateSensorManager mSensorManager;privateMySensorEventListener mListener;private int mStepDetector = 0; //自应用运行以来STEP_DETECTOR检测到的步数
private int mStepCounter = 0; //自系统开机以来STEP_COUNTER检测到的步数
@Overrideprotected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStepTV=(TextView)findViewById(R.id.tv_step);
mSensorManager=(SensorManager) getSystemService(Context.SENSOR_SERVICE);
mListener= newMySensorEventListener();
}
@Overrideprotected voidonResume() {
super.onResume();
mSensorManager.registerListener(mListener, mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR),
SensorManager.SENSOR_DELAY_NORMAL);
mSensorManager.registerListener(mListener, mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER),
SensorManager.SENSOR_DELAY_NORMAL);
}
@Overrideprotected voidonPause() {
super.onPause();
mSensorManager.unregisterListener(mListener);
}classMySensorEventListener implements SensorEventListener {
@Overridepublic void onSensorChanged(SensorEvent event) {
System.out.println("@@@:"+event.sensor.getType()+"--"+Sensor.TYPE_STEP_DETECTOR+"--"+Sensor.TYPE_STEP_COUNTER);if (event.sensor.getType() ==Sensor.TYPE_STEP_DETECTOR) {if (event.values[0] == 1.0f) {
mStepDetector++;
}
}else if (event.sensor.getType() ==Sensor.TYPE_STEP_COUNTER) {
mStepCounter= (int) event.values[0];
}
String desc= String.format("设备检测到您当前走了%d步,自开机以来总数为%d步", mStepDetector, mStepCounter);
mStepTV.setText(desc);
}
@Overridepublic void onAccuracyChanged(Sensor sensor, intaccuracy) {
}
}
}