I wanted to track sensor data (accelerometer, gyro) when user is moving/ phone is not stationary.
Things I was able to do :
- Listen to sensor data using Sensor Listener
sensorManager = getSystemService(SENSOR_SERVICE) as SensorManager
val accelerometerSensor =
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
sensorManager.registerListener(
accSensor,
accelerometerSensor,
SensorManager.SENSOR_DELAY_NORMAL
)
- Run a foreground service that always keeps running in the background to track the sensors even when the app is swiped off from recent apps
serviceIntent = Intent(context, SensorService::class.java)
context.startForegroundService(serviceIntent)
- Start the service as soon as the app is rebooted using broadcast listeners which listen to Boot completed event.
But I was not able to:
- Stop sensor service that collects sensor data when the device stops moving.
- Start sensor (which is not running) when the user's device starts to move and collect sensor data.
What might be the way to receive the Motion start and Motion end notifications/callback from the system so that we can decide to start/end foreground services.
from Collect sensor data only when user is moving
No comments:
Post a Comment