Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 484
user1163234 Avatar asked Dec 21 '15 11:12

user1163234


People also ask

How do I Start Counting my Steps?

Once you have your SMART goal, you’re ready to start counting your steps! The easiest way to start counting and tracking your steps over time is by using your cell phone! Most smartphones have a health app already installed included and equipped with a pedometer.

How do you count steps on your phone without going anywhere?

Hold your phone and swing your arm back and forth. This is an easy way to simulate steps without going anywhere. Grab your phone tightly in one hand while sitting or standing. Simply swing your arm back and forth to rack up the steps! [1] If your arm gets tired, just pass the phone to your other hand and keep going.

Why should I Count my daily steps?

By using a device to count your daily steps you will hold yourself accountable and be more likely to succeed in your step goal. If after dinner you notice you are just 300 steps shy of your goal, you’re more likely to take a few trips up and down the stairs to reach that goal.

How can I see how many steps I have taken?

On iPhones, you can swipe on your home and lock screen to view the app's widget — it conveniently shows you how many steps you've logged at a glance. Plus, people who use wheelchairs can use this step counter app as long as they have an Apple Watch.


Video Answer


1 Answers

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.

like image 183
cgr Avatar answered Oct 07 '22 21:10

cgr