相信大家对微信,小米手环这两个东西都不陌生,而且这两个东西都提供了一个计数器的功能,统计你每天行走的步数,可能有的人每天最开心的事就是看到自己又霸占了微信步数排行榜第一,离瘦瘦瘦真的是越来越近了。

今天我们就来看一看Android中的计步器应用是怎么实现的。

硬件支持

首先如果要能够实现计算步数的功能,是需要hardware支持的,即你的设备(手机/手环)都需要有计步器硬件的支持。

在android中我们可以通过命令查看自己的设备是否支持了计步功能:

adb shell pm list features
feature:android.hardware.sensor.stepcounter
feature:android.hardware.sensor.stepdetector

如果feature中有如上两个,则说明该设备是支持计步功能的。

好了,有了上面的feature作为基础,接下来我们就可以实现简单的计步器了。

Manifest清单文件

下面简单列一下我的app配置文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="linhui.skysoft.com.sensortestdemo">

    <uses-feature android:name="android.hardware.sensor.stepcounter"/>
    <uses-feature android:name="feature:android.hardware.sensor.stepdetector"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

清单中指定我们需要使用计步器的功能,这个将在PackageManager安装时解析出来。

UI界面

下面贴出我的UI代码,超简单......

<?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"
    android:gravity="center"
    android:orientation="vertical"
    tools:context="linhui.skysoft.com.sensortestdemo.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="好厉害"
        android:textSize="25dp"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_step"
        android:textSize="25dp"
        app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>


基本上简单写个显示步数的框就可以了,当然具体的商业app界面可能更花哨一点啦。

具体业务逻辑

package xxx.com.sensortestdemo;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import java.util.List;

public class MainActivity extends AppCompatActivity implements SensorEventListener {

    private final String TAG = "tiny";
    SensorManager mSensorManager;
    Sensor stepCounter;

    float mSteps = 0;
    TextView tv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 获取SensorManager管理器实例
        mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);

        // getSensorList用于列出设备支持的所有sensor列表
        List<Sensor> sensorList = mSensorManager.getSensorList(Sensor.TYPE_ALL);
        Log.i(TAG,"Sensor size:"+sensorList.size());
        for (Sensor sensor : sensorList) {
            Log.i(TAG,"Supported Sensor: "+sensor.getName());
        }

        tv = (TextView)findViewById(R.id.tv_step);

        // 获取计步器sensor
        stepCounter = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
        if(stepCounter != null){
            // 如果sensor找到,则注册监听器
            mSensorManager.registerListener(this,stepCounter,1000000);
        }
        else{
            Log.e(TAG,"no step counter sensor found");
        }
    }

    // 实现SensorEventListener回调接口,在sensor改变时,会回调该接口
    // 并将结果通过event回传给app处理
    @Override
    public void onSensorChanged(SensorEvent event) {
        mSteps = event.values[0];
        Log.i(TAG,"Detected step changes:"+event.values[0]);
        tv.setText("您今天走了"+String.valueOf((int)mSteps)+"步");
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
       // Log.i(TAG,"onAccuracyChanged");
    }

    protected void onPause() {
        // if unregister this hardware will not detected the step changes
        // mSensorManager.unregisterListener(this);
        super.onPause();
    }

    protected void onResume() {
        super.onResume();
    }
}


接下来就可以运行我们的app啦。

通过打印,我们知道当前设备支持的sensor如下:

BMI160 Accelerometer
BMI160 Gyroscope
BMI160 Gyroscope Uncalibrated
BMI160 Accelerometer -Wakeup Secondary
BMI160 Gyroscope -Wakeup Secondary
BMI160 Gyroscope Uncalibrated -Wakeup Secondary
Gravity
Linear Acceleration
Step Detector
Step Counter
Significant Motion Detector
Game Rotation Vector
Tilt Detector
Gravity -Wakeup Secondary
Linear Acceleration -Wakeup Secondary
Step Detector -Wakeup Secondary
Step Counter -Wakeup Secondary
Game Rotation Vector -Wakeup Secondary
AMD
RMD -WskeUp
Basic Gestures
Facing
Pedometer
Motion Accel
Coarse Motion Classifier
Speed Pulse

我们实现计步器功能的两个sensor也在其中。

当我们摇晃手机,或者行走时,下图中的数字反映的就是这个设备当前移动的步数了。

android studio如何制作步数记录器_ide

     

android studio如何制作步数记录器_xml_02

关于计步器的硬件实现原理,我们将在接下来的文章中继续深入挖掘。

最后,博主祝大家都能瘦瘦瘦,瘦出新宽度 ^_^