Android计步功能的实现代码-创新互联
本文对原文计步项目进行了精简,移除了进程服务和计时、守护进程、数据库保存等等,方便扩展功能。

Android4.4以上版本,有些手机有计步传感器可以直接使用, 而有些手机没有,但有加速度传感器,也可以实现计步功能(需要计算加速度波峰波谷来判断人走一步)!
一.使用
public class MainActivity extends AppCompatActivity implements StepCallBack{
.........
@Override
public void Step(int stepNum) {
// 计步回调
stepText.setText("步数:" + stepNum);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
stepText = (TextView) findViewById(R.id.step_text);
// 开启计步监听, 分为加速度传感器、或计步传感器
stepSensor = new StepSensorPedometer(this, this);
if (!stepSensor.registerStep()) {
Toast.makeText(this, "计步传传感器不可用!", Toast.LENGTH_SHORT).show();
stepSensor = new StepSensorAcceleration(this, this);
if (!stepSensor.registerStep()) {
Toast.makeText(this, "加速度传感器不可用!", Toast.LENGTH_SHORT).show();
}
}
}
.......
}
文章标题:Android计步功能的实现代码-创新互联
本文路径:http://www.jxjierui.cn/article/eehdi.html


咨询
建站咨询
