Android簡單的計步器應用實現

相信大家對微信,小米手環這兩個東西都不陌生,而且這兩個東西都提供了一個計數器的功能,統計你每天行走的步數,可能有的人每天最開心的事就是看到自己又霸佔了微信步數排行榜第一,離瘦瘦瘦真的是越來越近了。

今天我們就來看一看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也在其中。

當我們搖晃手機,或者行走時,下圖中的數字反映的就是這個設備當前移動的步數了。

     

關於計步器的硬件實現原理,我們將在接下來的文章中繼續深入挖掘。

最後,博主祝大家都能瘦瘦瘦,瘦出新寬度 ^_^ 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章