Search code examples
androidandroid-sensorsandroid-background

Android: Is there any way to collect data from the Significant Motion sensor when the app is in the background?


I'm trying to implement an application that collects and stores events from the Significant Motion trigger sensor on Android as detailed here.

What I am after is a way to implement this such that when events arrive from the sensor the app collects and records them, even when the app is not in the foreground. It feels like this really should be possible since the Activity Recognition API (which I understand relies on the Significant Motion Sensor) allows this by letting me pass a PendingIntent to be called when activity transitions occur. But the TriggerEventListener mechanism requires the listening process to be running, which is pretty much impossible to do continuously with background execution limits.

Am I missing anything? Is there any way to collect data from this (supposedly low-powered, battery-efficient, non-wakelock-requiring) sensor if my app is in the background?


Solution

  • In newer versions of Android, you cannot run the background services anymore and hence you cannot get the motion sensor data from your device when the application is not in the foreground. For more information please have a look here.

    However, you might consider implementing similar behaviour using a JobScheduler. Here's how you can implement one.

    Hope that helps.