0

I am making a battery app to monitor user's charging behavior. Basically, it is a background log server that saves your charge profile and analyzes it. Since it is possible that users is using their phones (e.g., playing games) during the charge , this changes the charge behavior. Thus it is necessary for my app to differentiate those data from the others.

Detecting if people is using the phone by a foreground app is trivial (you just check if user is touching your UI). However, I found it is relatively challenging for a background service to do the same thing. The only way comes to my mind is to check the brightness of screen. However, it is also possible that people leave the phone with screen on.

Could anyone give me some suggestions on this problem?

--------- Hi, here is a update of my question --------

First of all, the trick to get foreground app does not work after Android 5.0 (it is also posted getRunningTasks doesn't work in Android L). After Android 5.0, the "getRunningAppProcesses()" only returns your app. Based on the other discussion in Stackoverflow, There is an alternative to use app statistic but which needs system permission. Moreover, it also doesn't work at certain phones (in my test, most Samsung phone is unable to find the option to enable this function).

Second, I test the foreground app trick in some of my phones (before 5.0), but I think it doesn't meet the purpose of knowing if users are "interacting" the phone. For example, it is possible that users are running an app and just walking away for more than few hours but the foreground app trick still thinks the phone is in using (but it is not).

Community
  • 1
  • 1
Yu-Chih Tung
  • 81
  • 1
  • 9
  • Check if you can do it via http://developer.android.com/reference/android/app/ActivityManager.html, not sure about it though. – Yash Feb 19 '16 at 02:17
  • Possible duplicate of [Determining the current foreground application from a background task or service](http://stackoverflow.com/questions/2166961/determining-the-current-foreground-application-from-a-background-task-or-service) – Yash Feb 19 '16 at 02:28

1 Answers1

0

You can do it via ActivityManager.

Look at this piece of code :

Edit :

As of lollipop, getRunningTasks() has been depreciated. So, better use getRunningAppProcesses().

Sample code :

ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses == null) {
    return;
}
for (RunningAppProcessInfo appProcess : appProcesses) {
  if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
    //do what you want to do
  }
}

Credits for sample code : https://stackoverflow.com/a/8490088/1594776

Community
  • 1
  • 1
Yash
  • 5,225
  • 4
  • 32
  • 65
  • Hi. I found this code doesn't work. When I run my app. It find my app. But when I use other applications. It always can't find the process has IMPORTANCE_FOREGROUND property. – Yu-Chih Tung Feb 20 '16 at 01:25
  • Can you post your code here? because the api says it should do so : http://developer.android.com/reference/android/app/ActivityManager.html#getRunningAppProcesses() – Yash Feb 20 '16 at 05:47
  • But as said by the api, these methods are very much not intended for doing anything except presenting UI to the user about what is running. Do not rely on them to drive logic in your app. – Yash Feb 20 '16 at 05:49
  • Hi Yashasvi, I think this method is deprecated after Android 5.0: http://stackoverflow.com/questions/24625936/getrunningtasks-doesnt-work-in-android-l – Yu-Chih Tung Feb 21 '16 at 16:57
  • Yeah, saw that. I went through https://code.google.com/p/android-developer-preview/issues/detail?id=29, but couldn't find a straight forward solution for this. This is annoying that they have removed the support for this. Did you find any other solution? – Yash Feb 22 '16 at 03:53
  • Hi, I remember in the post thread, there is some ways to have a shell script to get this from system information, but I dont think it is a reliable way though... – Yu-Chih Tung Feb 23 '16 at 21:11
  • @Yu-ChihTung: hmm, right. If somehow you manage to achieve it, post that as a answer to this question, so that coming users won't have to search so much to get to the solution. – Yash Feb 24 '16 at 05:17
  • Sure. Thanks for your response – Yu-Chih Tung Feb 24 '16 at 16:58