代码如下:
package com.example.myjibuqi;
import androidx.appcompat.app.AppCompatActivity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
/*
利用手机的传感器实现一个计步器
*/
public class MainActivity extends AppCompatActivity implements SensorEventListener,View.OnClickListener {
boolean flag = true;
double lastPoint = 0;
TextView textView;
int count = 0;
SensorManager sm;
private Button btn1;
private Button btn2;
private Button btn3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.text);
btn1 = findViewById(R.id.button1);
btn2 = findViewById(R.id.button2);
btn3 = findViewById(R.id.button3);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
}
@Override
public void onSensorChanged(SensorEvent event) {
if(flag){
lastPoint = event.values[1];
flag = false;
}
//当两个values[1]之间的值大于8时则算是走了一步
if((Math.abs(event.values[1]) - lastPoint)>8){
//保存最后一步时values[1]的峰值
lastPoint = event.values[1];
//将当前的计数显示在textview组件里
textView.setText(String.valueOf(++count));
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
//暂时不设置
}
@Override
public void onClick(View view) {
String msg = "";
switch (view.getId()){
case R.id.button1:
/*
开始计数
*/
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
//注册传感器
sm.registerListener(this,sm.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_FASTEST);
msg = "已经开始计步器";
Toast.makeText(this, "111", Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
/*
重置计步器
*/
count = 0;
msg = "已经重置计步器";
Toast.makeText(this, "222", Toast.LENGTH_SHORT).show();
break;
case R.id.button3:
/*
停止计步器
*/
//注销传感器
sm.unregisterListener(this);
msg = "已经停止计步器";
Toast.makeText(this, "333", Toast.LENGTH_SHORT).show();
break;
}
textView.setText(String.valueOf(count));
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="10dp"
android:background="@color/cardview_shadow_start_color"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始"
android:layout_weight="1"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="重置"
android:layout_weight="1"
android:layout_marginLeft="5dp"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止"
android:layout_weight="1"
android:layout_marginLeft="5dp"/>
</LinearLayout>
<TextView
android:id="@+id/text"
android:text="0"
android:textSize="100sp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/design_default_color_background"/>
</LinearLayout>
这是完善后的代码:
代码可能出现了以下问题:
- 使用的是 **Sensor.TYPE_ORIENTATION** 传感器,这个传感器已经被弃用,不建议使用。应该使用 **Sensor.TYPE_ACCELEROMETER** 或 **Sensor.TYPE_STEP_COUNTER** 或 **Sensor.TYPE_STEP_DETECTOR** 传感器来实现计步器功能。
- 计步逻辑可能不够准确,您只是判断了两个 values[1] 之间的值是否大于8,这可能会导致误判或漏判,应该考虑加速度的变化率,峰值检测,精度范围等因素来提高计步的准确性²³。
- 代码没有处理传感器的精度变化,应该在 **onAccuracyChanged** 方法中添加相应的逻辑。
```java
package com.example.myjibuqi;
import androidx.appcompat.app.AppCompatActivity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
/*
利用手机的传感器实现一个计步器
*/
public class MainActivity extends AppCompatActivity implements SensorEventListener,View.OnClickListener {
boolean flag = true;
double lastPoint = 0;
TextView textView;
int count = 0;
SensorManager sm;
private Button btn1;
private Button btn2;
private Button btn3;
// 新增变量
private double original_value; // 初始值
private double current_value; // 当前值
private boolean motionState = true; // 是否处于运动状态
private boolean processState = false; // 是否已经开始计步
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.text);
btn1 = findViewById(R.id.button1);
btn2 = findViewById(R.id.button2);
btn3 = findViewById(R.id.button3);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
}
@Override
public void onSensorChanged(SensorEvent event) {
if(flag){
lastPoint = event.values[1];
flag = false;
}
// 修改计步逻辑
double range = 1; // 设置一个精度范围
float[] value = event.values;
current_value = magnitude(value[0], value[1], value[2]); // 计算当前的模
// 向上加速的状态
if (motionState == true) {
if (current_value >= last_value)
last_value = current_value;
else {
// 检测到一次峰值
if (Math.abs(current_value - last_value) > range) {
original_value = current_value;
motionState = false;
}
}
}
// 向下加速的状态
if (motionState == false) {
if (current_value <= last_value)
last_value = current_value;
else {
// 检测到一次峰值
if (Math.abs(current_value - last_value) > range) {
original_value = current_value;
if (processState == true) {
count++; // 检测到开始记录,步数加1
if (processState == true) {
textView.setText(String.valueOf(count)); // 更新读数
}
}
motionState = true;
}
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// 处理传感器精度变化
switch (accuracy) {
case SensorManager.SENSOR_STATUS_ACCURACY_HIGH:
Toast.makeText(this, "高精度", Toast.LENGTH_SHORT).show();
break;
case SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM:
Toast.makeText(this, "中等精度", Toast.LENGTH_SHORT).show();
break;
case SensorManager.SENSOR_STATUS_ACCURACY_LOW:
Toast.makeText(this, "低精度", Toast.LENGTH_SHORT).show();
break;
case SensorManager.SENSOR_STATUS_UNRELIABLE:
Toast.makeText(this, "不可靠", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
@Override
public void onClick(View view) {
String msg = "";
switch (view.getId()){
case R.id.button1:
/*
开始计数
*/
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
// 修改传感器类型为加速度传感器
sm.registerListener(this,sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_FASTEST);
msg = "已经开始计步器";
processState = true; // 设置开始标志为true
Toast.makeText(this, "111", Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
/*
重置计步器
*/
count = 0;
msg = "已经重置计步器";
processState = false; // 设置开始标志为false
Toast.makeText(this, "222", Toast.LENGTH_SHORT).show();
break;
case R.id.button3:
/*
停止计步器
*/
// 注销传感器
sm.unregisterListener(this);
msg = "已经停止计步器";
processState = false; // 设置开始标志为false
Toast.makeText(this, "333", Toast.LENGTH_SHORT).show();
break;
}
textView.setText(String.valueOf(count));
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
// 计算向量的模长
private double magnitude(float x, float y, float z) {
return Math.sqrt(x * x + y * y + z * z);
}
}