Domanda

I want to make a service that runs in the background and captures motion data using the motion sensors (such as accelerometer) when the user is using other applications. I also want to able to differentiate motion data sets based on what app was running at the time. For example, the output of the service could be:

App A : [Data set taken for the motion sensor while app A was running]
App B : [Data set taken for the motion sensor while app B was running]
...

I am comfortable with running a service in the background that captures the motion data; however, I don't know how to separate that data based on what app was in the foreground. Does anyone know how to achieve this? How do you find out what application was launched/moved to the foreground and when, then update the background service so it stores the data under that app's data set?

È stato utile?

Soluzione

It sounds like you just want your service to find what the foreground Activity is. You can find that information in other questions on StackOverflow, like this one. In short, you will want to use ActivityManager.getRunningTasks(). The topmost Activity is the first item in the List.

It sounds like you will also need to request the appropriate permission in your app's manifest.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top