本文的合集已經(jīng)編著成書猪叙,高級Android開發(fā)強化實戰(zhàn),歡迎各位讀友的建議和指導(dǎo)仁卷。在京東即可購買:https://item.jd.com/12385680.html
1. 集成計步算法的服務(wù)
集成計步算法的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)建時, 調(diào)用初始化方法initAppService()
; 結(jié)束時, 調(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ù)
在需要計步時, 啟動PedometerCounterService
服務(wù).
startService(new Intent(this, PedometerCounterService.class));
5. 設(shè)置參數(shù)
在啟動服務(wù)后, 設(shè)置參數(shù).
是否啟用喚醒鎖, 啟動后, 數(shù)據(jù)精確, 但是費電;
是否顯示庫的Log信息;
是否顯示默認的通知欄;
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è)置顯示默認通知欄
sendBroadcast(intent);
設(shè)置初始步數(shù)
Intent stepCountIntent = new Intent(IntentConsts.SET_OFFSET_DATA_FILTER);
stepCountIntent.putExtra(IntentConsts.SCS_OFFSET_DATA_EXTRA, 1000);
sendBroadcast(stepCountIntent);
設(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>
<!--計步傳感器值修改的廣播-->
<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ù)與運動時間
private void doAfterBroadcast(Intent intent) {
switch (action) {
// 計步傳感器的步數(shù)增加廣播
case IntentConsts.STEP_COUNTER_SENSOR_VALUE_FILTER:
// 計步傳感器的步數(shù)
mCYStepCounterSensorValue = intent.getIntExtra(IntentConsts.CY_STEP_SENSOR_VALUE_EXTRA, 0);
// 計步傳感器的時間
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);
// 加速度傳感器的時間
mASMotionTime = intent.getLongExtra(IntentConsts.MOTION_TIME_ACCELERATE_EXTRA, 0L);
break;
// ...
}
}
默認選擇計步傳感器, 當(dāng)沒有計步傳感器時, 使用加速度傳感器記錄. 內(nèi)部控制.
6. 停止與啟動服務(wù)的自啟動
服務(wù)會自動啟動, 防止因系統(tǒng)原因的關(guān)閉. 如需關(guān)閉服務(wù)的自啟動, 需要設(shè)置; 再次開啟, 也需要重新設(shè)置. 默認開啟自啟動服務(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.** { *; }
補充
使用說明補充
(1) 啟動服務(wù), 在application里啟動還是普通activity?
當(dāng)需要計步時, 啟動服務(wù)即可, 位置都可以.
(2) 設(shè)置參數(shù)在application里設(shè)置還是普通activity?
同上, 在服務(wù)啟動后, 設(shè)置參數(shù).
(3) 獲取步數(shù)和時間doAfterBroadcast, 這個方法如果在MainActiity里調(diào)用了, 是不是只要走路, 就一直會收到廣播呢?
確保廣播可以收到, 只要有步數(shù)變化, 就會收到.
(4) 好浪費電, 有什么辦法可以不費電?
電量優(yōu)化措施已經(jīng)封裝在服務(wù)中, 不需要優(yōu)化. 電量消耗已經(jīng)最小.
(5) 你們會不會存到數(shù)據(jù)庫里, 如果想獲取數(shù)據(jù)可不可以調(diào)用方法直接從數(shù)據(jù)庫里獲取每天的運動步數(shù)呢, 以及如果獲取最近5天的歷史步數(shù)呢?
最好做本地存儲, 不依賴服務(wù)的存儲, 統(tǒng)計本地的歷史數(shù)據(jù), 不推薦直接操作服務(wù)的數(shù)據(jù)庫.
不過數(shù)據(jù)庫的接口也已經(jīng)開放, 如下獲取計步傳感器與加速度傳感器的步數(shù), 以此類推, 風(fēng)險較大.
// 獲取5天前計步傳感器存儲步數(shù)
int stepSensor = CYStepSensorRecordDB.getCurrentStep(MainUtils.getBeforeDay(new Date(), 5));
// 獲取5天前加速度傳感器存儲步數(shù)
int accelerateSensor = CYAccelerateRecordDB.getCurrentStep(MainUtils.getBeforeDay(new Date(), 5));
// 判斷是否包含計步傳感器
boolean isStepSensor = MainUtils.hasStepSensor(getApplicationContext());
(6) 出現(xiàn)數(shù)據(jù)庫加載錯誤, 怎么辦?
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.0與SugarORM沖突, 通過設(shè)置Instant Run解決.
重新運行應(yīng)用.
合作
本算法目前僅在公司內(nèi)部使用, 如需合作請直接站內(nèi)私信我.
OK, that's all.