0

我有一部三星手机,它显然没有计步器(计步器),因为当我使用 TYPE_STEP_COUNTER 时,它说没有计步器。所以在网上搜索后,我遇到了一个使用加速度计审查器的实现。但问题是实现在检测步骤方面不是很准确。我试图校准数字,但仍然不是很可靠。它将添加 x、y、z 轴的平方并计算根以获得步长的大小。所以我的问题是有没有人这样做,以及他们是如何以一种很好的实施方式做到的。谢谢这是我正在使用的代码

public class StatsFragment extends Fragment implements SensorEventListener {
    private static final String LOG_TAG = "STATS";
    private TextView stepCountTextView;
    private Button clearCounterButton;
    private Sensor stepSensor;
    private Integer stepCount = 0;
    private double MagnitudePrevious = 0;
    private SensorManager sensorManager;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_stats, container, false);

        stepCountTextView = view.findViewById(R.id.step_counter);
        clearCounterButton = view.findViewById(R.id.clear_button);

        sensorManager = (SensorManager) getActivity().getSystemService(getActivity().SENSOR_SERVICE);
        stepSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

        clearCounterButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                stepCount = 0;
                stepCountTextView.setText(stepCount.toString());
            }
        });

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        if(stepSensor != null) {
            sensorManager.registerListener(this, stepSensor, SensorManager.SENSOR_DELAY_UI);
        }
        else {
            Toast.makeText(getActivity(), "Sensor not available!", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        //unregister sensor
        sensorManager.unregisterListener(this);
    }

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        if (sensorEvent != null) {
            float x_acceleration = sensorEvent.values[0];
            float y_acceleration = sensorEvent.values[1];
            float z_acceleration = sensorEvent.values[2];
            double Magnitude = Math.sqrt(x_acceleration*x_acceleration + y_acceleration*y_acceleration
                    + z_acceleration*z_acceleration);
            double MagnitudeDelta = Magnitude - MagnitudePrevious;
            MagnitudePrevious = Magnitude;

            if (sensorEvent.values[0] > 6){
                stepCount++;
            }
            stepCountTextView.setText(stepCount.toString());
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {

    }
}
4

1 回答 1

0

在 Android 10 上,您需要请求权限才能访问步数传感器。将以下代码添加到Manifest.xml

<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>

并在onCreateView方法中添加如下代码:

if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACTIVITY_RECOGNITION) == PackageManager.PERMISSION_DENIED){
   //ask for permission
   requestPermissions(new String[]{Manifest.permission.ACTIVITY_RECOGNITION}, 0);
 }
于 2020-09-26T11:56:37.287 回答