計(jì)步算法庫的使用方式

歡迎Follow我的GitHub, 歡迎關(guān)于計(jì)步算法庫的算法原理使用方法.

Motion

本文的合集已經(jīng)編著成書,高級Android開發(fā)強(qiáng)化實(shí)戰(zhàn),歡迎各位讀友的建議和指導(dǎo)。在京東即可購買:https://item.jd.com/12385680.html

Android

1. 集成計(jì)步算法的服務(wù)

集成計(jì)步算法的AAR包

2. 添加權(quán)限

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />

3. 初始化服務(wù)

在應(yīng)用創(chuàng)建時(shí), 調(diào)用初始化方法initAppService(); 結(jié)束時(shí), 調(diào)用釋放方法releaseAppService().

public class PedometerApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        PedometerCounterService.initAppService(getApplicationContext());
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        PedometerCounterService.releaseAppService();
    }
}

用于存儲數(shù)據(jù)庫的初始化.

4. 啟動服務(wù)

在需要計(jì)步時(shí), 啟動PedometerCounterService服務(wù).

startService(new Intent(this, PedometerCounterService.class));

5. 設(shè)置參數(shù)

在啟動服務(wù)后, 設(shè)置參數(shù).

是否啟用喚醒鎖, 啟動后, 數(shù)據(jù)精確, 但是費(fèi)電;
是否顯示庫的Log信息;
是否顯示默認(rèn)的通知欄;

Intent intent = new Intent(IntentConsts.ALGORITHM_SET_PARAMETERS_FILTER);
intent.putExtra(IntentConsts.SWITCH_WAKE_LOCK_EXTRA, false); // 啟動喚醒鎖
intent.putExtra(IntentConsts.SWITCH_SHOW_LOGS_EXTRA, false); // 設(shè)置Log信息
intent.putExtra(IntentConsts.SWITCH_SHOW_NOTIFICATION_EXTRA, true); // 設(shè)置顯示默認(rèn)通知欄
sendBroadcast(intent);

設(shè)置初始步數(shù)

Intent stepCountIntent = new Intent(IntentConsts.SET_OFFSET_DATA_FILTER);
stepCountIntent.putExtra(IntentConsts.SCS_OFFSET_DATA_EXTRA, 1000);
sendBroadcast(stepCountIntent);

設(shè)置初始運(yùn)動時(shí)間

Intent motionTimeIntent = new Intent(IntentConsts.SET_MOTION_TIME_FILTER);
motionTimeIntent.putExtra(IntentConsts.SCS_MOTION_TIME_EXTRA, 20000L);
sendBroadcast(motionTimeIntent);

5. 接收數(shù)據(jù)

使用Receiver接收步數(shù)廣播.

<receiver
    android:name=".StepReceiver"
    android:exported="false">
    <intent-filter>
        <!--計(jì)步傳感器值修改的廣播-->
        <action android:name="me.chunyu.pedometer.step_counter_sensor_value_filter"/>
        <!--加速度傳感器值修改的廣播-->
        <action android:name="me.chunyu.pedometer.accelerate_sensor_value_filter"/>
        <!--日期修改的廣播-->
        <action android:name="me.chunyu.pedometer.time_change_filter"/>
    </intent-filter>
</receiver>

獲取步數(shù)與運(yùn)動時(shí)間

private void doAfterBroadcast(Intent intent) {
    switch (action) {
        // 計(jì)步傳感器的步數(shù)增加廣播
        case IntentConsts.STEP_COUNTER_SENSOR_VALUE_FILTER:
            // 計(jì)步傳感器的步數(shù)
            mCYStepCounterSensorValue = intent.getIntExtra(IntentConsts.CY_STEP_SENSOR_VALUE_EXTRA, 0);
            // 計(jì)步傳感器的時(shí)間
            mSCSMotionTime = intent.getLongExtra(IntentConsts.MOTION_TIME_STEP_SENSOR_EXTRA, 0L);
            break;

        // 加速度傳感器的步數(shù)增加廣播
        case IntentConsts.ACCELERATE_SENSOR_VALUE_FILTER:
            // 加速度傳感器的步數(shù)
            mCYAccelerateSensorValue = intent.getIntExtra(IntentConsts.CY_ACCELERATE_SENSOR_VALUE_EXTRA, 0);
            // 加速度傳感器的時(shí)間
            mASMotionTime = intent.getLongExtra(IntentConsts.MOTION_TIME_ACCELERATE_EXTRA, 0L);
            break;
        // ...
    }
}

默認(rèn)選擇計(jì)步傳感器, 當(dāng)沒有計(jì)步傳感器時(shí), 使用加速度傳感器記錄. 內(nèi)部控制.

6. 停止與啟動服務(wù)的自啟動

服務(wù)會自動啟動, 防止因系統(tǒng)原因的關(guān)閉. 如需關(guān)閉服務(wù)的自啟動, 需要設(shè)置; 再次開啟, 也需要重新設(shè)置. 默認(rèn)開啟自啟動服務(wù).

// 關(guān)閉自啟動服務(wù)
public void stopAuto(View view) {
    Context context = getApplicationContext();
    ConfigConsts.setAutoStartService(context, false);
    MainUtils.tryStopService(context); // 關(guān)閉服務(wù)
}

// 開啟自啟動服務(wù)
public void startAuto(View view) {
    Context context = getApplicationContext();
    ConfigConsts.setAutoStartService(context, true);
    MainUtils.tryStartService(context); // 啟動服務(wù)
}

7. 數(shù)據(jù)庫

導(dǎo)入Sugar

compile 'com.github.satyan:sugar:1.5'
-keep class me.chunyu.pedometerservice.database.** { *; }

補(bǔ)充

使用說明補(bǔ)充

(1) 啟動服務(wù), 在application里啟動還是普通activity?
當(dāng)需要計(jì)步時(shí), 啟動服務(wù)即可, 位置都可以.

(2) 設(shè)置參數(shù)在application里設(shè)置還是普通activity?
同上, 在服務(wù)啟動后, 設(shè)置參數(shù).

(3) 獲取步數(shù)和時(shí)間doAfterBroadcast, 這個(gè)方法如果在MainActiity里調(diào)用了, 是不是只要走路, 就一直會收到廣播呢?
確保廣播可以收到, 只要有步數(shù)變化, 就會收到.

(4) 好浪費(fèi)電, 有什么辦法可以不費(fèi)電?
電量優(yōu)化措施已經(jīng)封裝在服務(wù)中, 不需要優(yōu)化. 電量消耗已經(jīng)最小.

(5) 你們會不會存到數(shù)據(jù)庫里, 如果想獲取數(shù)據(jù)可不可以調(diào)用方法直接從數(shù)據(jù)庫里獲取每天的運(yùn)動步數(shù)呢, 以及如果獲取最近5天的歷史步數(shù)呢?
最好做本地存儲, 不依賴服務(wù)的存儲, 統(tǒng)計(jì)本地的歷史數(shù)據(jù), 不推薦直接操作服務(wù)的數(shù)據(jù)庫.
不過數(shù)據(jù)庫的接口也已經(jīng)開放, 如下獲取計(jì)步傳感器與加速度傳感器的步數(shù), 以此類推, 風(fēng)險(xiǎn)較大.

// 獲取5天前計(jì)步傳感器存儲步數(shù)
int stepSensor = CYStepSensorRecordDB.getCurrentStep(MainUtils.getBeforeDay(new Date(), 5));

// 獲取5天前加速度傳感器存儲步數(shù)
int accelerateSensor = CYAccelerateRecordDB.getCurrentStep(MainUtils.getBeforeDay(new Date(), 5));

// 判斷是否包含計(jì)步傳感器
boolean isStepSensor = MainUtils.hasStepSensor(getApplicationContext()); 

(6) 出現(xiàn)數(shù)據(jù)庫加載錯(cuò)誤, 怎么辦?

java.lang.RuntimeException: 
Unable to create service me.chunyu.pedometerservice.PedometerCounterService: 
android.database.sqlite.SQLiteException: 
no such table: CY_ACCELERATE_RECORD (code 1): , 
while compiling: SELECT * FROM CY_ACCELERATE_RECORD WHERE _date = ?
...

Android Studio 2.0SugarORM沖突, 通過設(shè)置Instant Run解決.

ORM

重新運(yùn)行應(yīng)用.


合作

本算法目前僅在公司內(nèi)部使用, 如需合作請直接站內(nèi)私信我.


OK, that's all.

最后編輯于
?著作權(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)容