Best way to get daily steps count of user to send to server in background
I want to send to my server every 24 hours how many steps I walked. I have an AlarmManager to go off alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),1000*60*60*24, pendingIntent1)
Now this is where I am stuck... how should I measure the daily steps?(remember I need to send this in the background) I can use this a service that could override this function public void onSensorChanged(SensorEvent event)
this doesn't seem so accurate since my Service could be destroyed(and battery will get drained very fast).
Or
I can try and use the Google Fitness but I need to request connection from user every time I try to get the data mGoogleApiClient.connect()
(I need to get this data in the background)
Anyone have experience doing this?
onSensorChanged() does work properly. Sensor should be good enough in the device that it should not count vibration as a step. But the only problem while capturing the step count with onSencorChanged() is that if we shake the device it count it as a step. To avoid this, most of the solutions will implement tracking the location and send to the backend so that backend would calculate if the device has actually moved. It sends the actual step count back to device which can be displayed.
Leaving this, for your case if you think service-gets-destroyed is your concern, you can restart it in a guaranteed way. See my answer to Unable to restart the service. This explains how to restart service using UncaughtExceptionHandler when app is killed through recent tasks or by system in low memory situation. Your service does not run in deep sleep mode. You can get it run using WakefulBroadcastReceiver and startWakefulService().
You should reduce the frequency of sending data to backend to as less as possible to avoid draining battery.
I am not sure of GoogleFit API. Research on this for yourself and you can add answer to your question. ;-)
UPDATE:
Nice articles on sensors in android - How Android devices cope with continuous sensing in standby mode (screen off) and Suspend Mode.
Nice point to note :
“Suspend” is a low-power mode where the SoC (System on Chip) is not powered. The power consumption of the device in this mode is usually 100 times less than in the “On” mode.
链接地址: http://www.djcxy.com/p/89376.html