7

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?

Rajesh Satvara
  • 3,842
  • 2
  • 30
  • 50
user1163234
  • 2,407
  • 6
  • 35
  • 63

1 Answers1

4

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:
Read Suspend Mode about sensors in android.

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.

cgr
  • 4,578
  • 2
  • 28
  • 52
  • This is great thx! However, I cant use the server for location and all the calculation needs to be done on the client side. – user1163234 Dec 21 '15 at 14:16
  • @user1163234, Good it was helpful. As you need to provide the right step count (otherwise it irks user very badly) , track the location too and count it within device. – cgr Dec 21 '15 at 14:20
  • @user1163234 You may accept and/or vote up answer.. ;-) – cgr Dec 21 '15 at 14:51
  • link to mksens.com is broken (first link after "UPDATE"). it seems like the site is no longer active / updated. the resource is still reachable on web.archive.org with the link provided in the answer though. https://web.archive.org/web/20160306172404/http://www.mksens.com/how-android-devices-cope-with-continuous-sensing-in-standby-mode-screen-off/ – V4karian Sep 01 '20 at 21:59