Androidmianifest.xml
<uses-feature android:name="android.hardware.sensor.stepcounter"/> <uses-feature android:name="feature:android.hardware.sensor.stepdetector"/>
MianActivity.java
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 class MainActivity extends Activity { private TextView mStepTV; private SensorManager mSensorManager; private MySensorEventListener mListener; private int mStepDetector = 0; // 自应用运行以来STEP_DETECTOR检测到的步数 private int mStepCounter = 0; // 自系统开机以来STEP_COUNTER检测到的步数 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mStepTV = (TextView)findViewById(R.id.tv_step); mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); mListener = new MySensorEventListener(); } @Override protected void onResume() { 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); } @Override protected void onPause() { super.onPause(); mSensorManager.unregisterListener(mListener); } class MySensorEventListener implements SensorEventListener { @Override public 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); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } } }
layout中
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/tv_step" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="走一走,看看有什么" android:textSize="20sp" /> </LinearLayout>
来源:https://www.cnblogs.com/837634902why/p/12174228.html