使用Android4.4 Kitkat 新增的STEP DETECTOR 以及 STEP COUNTER傳感器机杜。
官方介紹:
TYPE_STEP_COUNTER:計(jì)步器(記錄歷史步數(shù)累加值)
int TYPE_STEP_COUNTER
A constant describing a step counter sensor.
A sensor of this type returns the number of steps taken by the user since the last reboot while activated. The value is returned as a float (with the fractional part set to zero) and is reset to zero only on a system reboot. The timestamp of the event is set to the time when the last step for that event was taken. This sensor is implemented in hardware and is expected to be low power. If you want to continuously track the number of steps over a long period of time, do NOT unregister for this sensor, so that it keeps counting steps in the background even when the AP is in suspend mode and report the aggregate count when the AP is awake. Application needs to stay registered for this sensor because step counter does not count steps if it is not activated. This sensor is ideal for fitness tracking applications. It is defined as an REPORTING_MODE_ON_CHANGEsensor.
See SensorEvent.values for more details.
Constant Value: 19 (0x00000013)
TYPE_STEP_DETECTOR:檢測(cè)器(檢測(cè)每次步伐數(shù)據(jù))
int TYPE_STEP_DETECTOR
A constant describing a step detector sensor.
A sensor of this type triggers an event each time a step is taken by the user. The only allowed value to return is 1.0 and an event is generated for each step. Like with any other event, the timestamp indicates when the event (here the step) occurred, this corresponds to when the foot hit the ground, generating a high variation in acceleration. This sensor is only for detecting every individual step as soon as it is taken, for example to perform dead reckoning. If you only need aggregate number of steps taken over a period of time, register for TYPE_STEP_COUNTER instead. It is defined as a REPORTING_MODE_SPECIAL_TRIGGER sensor.
See SensorEvent.values for more details.
Constant Value: 18 (0x00000012)
使用內(nèi)容:
Sensor:
SensorEvent:
SensorManager:
SensorEventListener:
使用:
1帜讲、使用傳感器之前首先獲取SensorManager通過(guò)系統(tǒng)服務(wù)獲取:
SensorManager mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
2椒拗、獲取我們需要的傳感器類(lèi)型:
//單次有效計(jì)步
Sensor mStepCount = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
//系統(tǒng)計(jì)步累加值
Sensor mStepDetector = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);
3似将、注冊(cè)監(jiān)聽(tīng)者(監(jiān)聽(tīng)傳感器事件)
mSensorManager.registerListener(this, mStepDetector, SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(this, mStepCount, SensorManager.SENSOR_DELAY_FASTEST);
PS:取消注冊(cè):
mSensorManager.unregisterListener(this, mStepDetector);
mSensorManager.unregisterListener(this, mStepCount);
4、實(shí)現(xiàn)SensorEventListener接口蚀苛,重寫(xiě)方法并獲取數(shù)據(jù):
從監(jiān)聽(tīng)到的傳感器事件中選取合適的類(lèi)型在验,獲得數(shù)據(jù):
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == *sensorTypeC*) {
//event.values[0]為計(jì)步歷史累加值
tvAllCount.setText(event.values[0] + "步");
}
if (event.sensor.getType() == *sensorTypeD*) {
if (event.values[0] == 1.0) {
mDetector++;
//event.values[0]一次有效計(jì)步數(shù)據(jù)
tvTempCount.setText(mDetector + "步");
}
}
}
Summary
1、計(jì)步器數(shù)據(jù)會(huì)在手機(jī)重啟后清零堵未,因此此處需要注意根據(jù)需要來(lái)做數(shù)據(jù)保護(hù)腋舌。
2、計(jì)步器啟動(dòng)需要在檢測(cè)器啟動(dòng)的基礎(chǔ)上才能實(shí)現(xiàn)兴溜,因此要先啟動(dòng)檢測(cè)器侦厚。
3、
資料:
Android4.4 新增傳感器
http://blog.objcc.com/android-4-4-kitkat-sensor-batching/
官方文檔:
https://developer.android.com/reference/android/hardware/Sensor.html#STRING_TYPE_STEP_COUNTER
android中的計(jì)步問(wèn)題及計(jì)步傳感器分析:
http://www.cfanz.cn/index.php?c=article&a=read&id=250334
一個(gè)簡(jiǎn)單的計(jì)步器使用Demo:
http://blog.csdn.net/aikongmeng/article/details/40457233
另一種方法實(shí)現(xiàn)計(jì)步數(shù)據(jù)
http://blog.csdn.net/finnfu/article/details/45273183