Android step counting module example code (similar to wechat movement)

Recently, I studied the step counting module in the project and began to record the steps of the day at 0:00 every day, similar to wechat movement. I've encountered many pits. I have time to sort them out for you to see today. Before doing this, I searched Google, Baidu and GitHub and didn't find any good ones. Most of them need to survive in the background and need background service.

For the major mobile phone manufacturers now, in order to improve the battery life (power saving), basically alertmanager

android.intent.action.BOOT_ Both completed and background services were killed.

The background survival strategy service is basically useless. It's only a matter of time before it is killed by the mobile phone system, so I think it's best not to do it. Even if the background survives, users will delete the app when they see that it costs a lot of power.

Project address: https://github.com/jiahongfei/TodayStepCounter

At present, there are two ways of Android step counting

System step counting chip

After Android version 4.4, some models have implemented sensor.type_ STEP_ Counter sensor is used to record the number of steps the user walks. The recording starts when the mobile phone is turned on, and is reset to 0 when the mobile phone is turned off.

This stepping chip is at the system level. Compared with the previous sensor stepping chip, its performance is optimized:

The app will not consume additional power because it uses the step recording function alone

The system chip step recording is continuous, which can optimize the problem of non step recording in the background of some models.

Calculation method of acceleration sensor

The acceleration sensor consumes very much power, resulting in high power consumption of the app and affecting the user experience. Real time running in the background is required to realize the step recording function. If the app process is killed by the system or security software, the step recording function cannot be used

Project address: https://github.com/jiahongfei/TodayStepCounter

Step counting is realized according to the above two methods. If the mobile phone provides a step counting sensor, sensor.type is used_ STEP_ Counter mode (APP background shutdown or step counting). If it is not provided, sensormanager.sensor is used_ DELAY_ UI mode (app needs to keep running in the background).

Project structure:

The step counting service uses a separate process, so it uses the inter process communication Aidl. Todaystopcounterlib is a library file used to implement the step counting algorithm in a separate process. The app relies on the todaystopcounterlib project to obtain the current step number display.

Access mode:

See the following code for how to use the step counting module in the project structure app

Step counting strategy:

1. If the accelerometer is used, the app must survive in the background to measure the step‘ 2. The highlight is to use the step counting sensor to count the steps. The app can also count the steps when it is closed in the background.

Sensor.type is adopted as follows_ STEP_ Counter sensor realizes step counting strategy:

1. When a user newly installs an app, the steps are counted from the first time the user opens the app, and the day does not span days

2. The user keeps opening the app to count steps, and does not close the app across 0 points

3. The user opens the app and closes it in the background once. The alertmanager can not start automatically when it crosses 0 points and is separated by 0 points (at present, most mobile phones cannot start)

4. When the user opens the app once, the background is closed. It spans multiple 0 points and the alertmanager can be started after 0 points are separated

5. The user starts the app once and restarts the mobile phone on the same day (self startup is not easy to use, and many mobile phones are not easy to use)

6. The user starts the app once and switches on and off across 0 points (it is difficult to use since startup)

7. The user starts the app once and switches on and off across 0 points (it can be started automatically)

defect

1. Scheme 3 calculates the steps of the previous day by opening the app across 0 points. If it spans more than one day, the steps of the previous day will be very large.

2. The number of steps before scheme 4 crosses 0 point will be lost (because 0 point is separated, alertmanager can call back, so it can process the data before 0 point. It will be repaired in later versions)

3. Call sharepreference frequently in pedometer callback to charge power

be careful:

1. Opening the app every morning can improve the accuracy of several steps, which is almost the same as the number of wechat steps.

2. Open the app every time you restart the phone, and the steps will be merged

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>