Android計(jì)步(簡單Demo)

18.03.01

附上Demo鏈接,歡迎探討撕咬~ Pedometer

18.02.23

最近接了個測試各大手機(jī)廠商計(jì)步芯片的活兒,所以簡單弄了個計(jì)步器的小Demo,搞搞測試。后續(xù)會把測試的結(jié)果一起發(fā)上來,造福鄉(xiāng)親們:我就是結(jié)果????

基本功能

實(shí)時(shí)顯示從系統(tǒng)StepCounter芯片獲取的步數(shù)。

如果你要做個精準(zhǔn)的計(jì)步器,只有基本步數(shù)的讀取與顯示是遠(yuǎn)遠(yuǎn)不夠的,還可能會涉及到:

  1. 進(jìn)程?;?br> 應(yīng)用被系統(tǒng)意外殺死會導(dǎo)致數(shù)據(jù)丟失等情況,進(jìn)程再次起來的時(shí)候需要有數(shù)據(jù)恢復(fù)等操作
  2. ACC與StepCounter模式篩選
    畢竟不是所有的手機(jī)都支持StepCounter芯片,在一些古老的手機(jī)上還是需要用ACC計(jì)算步數(shù)。對于兩者都支持設(shè)備,哪種方式更準(zhǔn)確呢,這就需要有個篩選策略了
  3. 高頻數(shù)據(jù)讀寫
    在統(tǒng)計(jì)數(shù)據(jù)中你會看到有的芯片會在1s內(nèi)回調(diào)60多次數(shù)據(jù),這個時(shí)候就要考慮讀寫性能了,回調(diào)一次就寫一次sp顯然不是個好主意

工程源碼

MainActivity - UI 顯示
public class MainActivity extends AppCompatActivity {

    private TextView tvStep;

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

        init();
    }

    private void init() {
        tvStep = findViewById(R.id.tv_step);

        register();
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    private void register() {
        StepCounterManager.getInstance().addStepCounterObserver(new Observer() {
            @Override
            public void update(Observable o, Object arg) {
                tvStep.setText("芯片實(shí)時(shí)獲取步數(shù): " + (float) arg );
            }
        });

        StepCounterManager.getInstance().register();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        StepCounterManager.getInstance().clearStepObserver();
        StepCounterManager.getInstance().unRegister();
    }
}
StepCounterManager - 核心StepCounter芯片注冊回調(diào)類
public class StepCounterManager implements SensorEventListener {

    private static final int SENSER_TYPE_C = Sensor.TYPE_STEP_COUNTER;

    private static StepCounterManager instance;

    private final String TAG = "StepCounterManager";

    private SensorManager mSensorManager;
    private Sensor mStepCount;
    private StepCounterObservable mStepCounterObservable;

    private StepCounterManager() {
        mSensorManager = (SensorManager) GlobalConfig.getAppContext().getSystemService(Context.SENSOR_SERVICE);

        if (mSensorManager == null) {
            Log.i(TAG, "StepCounterManager init error");
            return;
        }

        mStepCount = mSensorManager.getDefaultSensor(SENSER_TYPE_C);

        mStepCounterObservable = new StepCounterObservable();
    }

    static StepCounterManager getInstance() {
        if (instance == null) {
            synchronized (StepCounterManager.class) {
                if (instance == null) {
                    instance = new StepCounterManager();
                }
            }
        }

        return instance;
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    void register() {
        if (mStepCount == null) {
            return;
        }

        String info = "name = "
                + mStepCount.getName() + ", version = " + mStepCount.getVersion() + ", vendor = " + mStepCount.getVendor()
                + ", FifoMaxEventCount = " + mStepCount.getFifoMaxEventCount()
                + ", FifoReservedEventCount = " + mStepCount.getFifoReservedEventCount() + ", MinDelay = "
                + mStepCount.getMinDelay() + ", MaximumRange = " + mStepCount.getMaximumRange()
                + ", Power = " + mStepCount.getPower()
                + ", ReportingMode = " + mStepCount.getReportingMode() + ", Resolution = " + mStepCount.getResolution() + ", MaxDelay = " + mStepCount.getMaxDelay();


        Log.i(TAG, "芯片信息 : " + info);

        mSensorManager.registerListener(this, mStepCount, SensorManager.SENSOR_DELAY_FASTEST);
    }

    void unRegister() {
        mSensorManager.unregisterListener(this);
    }

    void addStepCounterObserver(Observer observer) {
        mStepCounterObservable.addObserver(observer);
    }

    void clearStepObserver() {
        mStepCounterObservable.deleteObservers();
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public void flush() {
        mSensorManager.flush(this);
    }

    private void setStepCount(float count) {
        mStepCounterObservable.sendChange(count);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() != SENSER_TYPE_C) {
            return;
        }

        setStepCount(event.values[0]);
    }

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

    }
}
StepCounterObservable - 可被監(jiān)測的StepCounter數(shù)據(jù)源
public class StepCounterObservable extends Observable {

    public void sendChange(float progress) {
        setChanged();
        notifyObservers(progress);
    }
}
activity_main.xml - 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_step"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容