I'm looking at implementing the step sensor API introduced in Android 4.4 (http://youtu.be/yv9jskPvLUc). However, I am unable to find a clear explanation on what the recommended way to monitor this in the background is? It seems like most examples only show how to do this with an activity while the app is running. I don't particularly need a high frequency of updates - I basically want to log the amount of steps the user has walked every hour to a backend service. Should I just spin up a background service that calls registerListener on SensorManager, or is there a more elegant way?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
This is not a complete solution, but the most energy-efficient way could be to wake up your device every hour or so, start a service which quickly reads the data, then goes back to sleep.
Depending on which device level you target, using a WakefulBroadcastReceiver, as described in this answer, seems the way to go.
You need to
If any of the points a less than clear, say so. See http://code.tutsplus.com/tutorials/android-barometer-logger-acquiring-sensor-data--mobile-10558
As far as I know, there is no way around the
SensorManager
, but if you need the data very infrequently, you could trigger the sensor manually and get its values with aTriggerEventListener
, which is a little cleaner than aSensorEventListener
.AlarmManager
is typically the best option for starting an hourly timer, and it works even if your app isn't running.AlarmManager
sends anIntent
to a class that extendsBroadcastReceiver
, and that class will start yourService
. TheAlarmManager
can be set anywhere in your app depending on your implementation.StepCountService
MainActivity
AlarmReceiver
@TheoKanning's answer is the correct way to do this manually. Alternatively, Google Fit continuously logs this data and has an API you can use to pull it into your app.