4

From within the code of a particular service I want to determine if the service is in the foreground. I looked at:

ActivityManager.RunningServiceInfo

specifically RunningServiceInfo.foreground, but the documentation says, "Set to true if the service has asked to run as a foreground process."

So can I rely on RunningServiceInfo.foreground? Or is there another way?

PS: I'm not having other issues, my service is running fine. This question is more out of curiosity. Already browsed ASOP and didn't see anything, but maybe I missed something...

If you have a similar question, this may help: How to determine if an Android Service is running in the foreground?

...Though I found the accepted solution to be incomplete.

Community
  • 1
  • 1
newbyca
  • 1,523
  • 2
  • 13
  • 25
  • what are you trying to accomplish by seeing if the service is running in the foreground? – tyczj Sep 10 '13 at 18:46
  • I was having a problem where I would call setForeground but the service was still getting killed under load. It turned out I wasn't calling setForeground correctly (passing it id=0). But before I realized that it occurred to me that I didn't actually know the service was in the foreground, just that I had requested it. So I looked for a way to determine if the service was foregrounded and couldn't find one. So thought I'd pose it to SO ... – newbyca Sep 10 '13 at 19:08
  • well its not setForeground its `startForeground()` and using that you need to create a notification that is displayed in the notification bar – tyczj Sep 10 '13 at 19:12
  • Sorry you're correct, I meant startForeground() not setForeground(). Yes, I'm aware it requires a notification, thanks. – newbyca Sep 10 '13 at 19:14
  • so I dont understand why you need to know if the service is running in the foreground or not, just to see if it has been killed? – tyczj Sep 10 '13 at 19:18
  • I don't *need* to know, I'm just curious. My service is working fine :) ... besides, how is my *need* to know relevant to a solution? – newbyca Sep 10 '13 at 19:19
  • to see if there is a better way or some other solution??? – tyczj Sep 10 '13 at 19:23
  • 1
    check here http://stackoverflow.com/questions/6452466/how-to-determine-if-an-android-service-is-running-in-the-foreground/6452570#6452570 – tyczj Sep 10 '13 at 19:25
  • Thanks, already checked it out though ... I actually commented there that the solution was possibly incomplete. – newbyca Sep 10 '13 at 19:27
  • 'to see if there is a better way or some other solution???' ... sorry, thought it was clear that I wasn't having other issues. – newbyca Sep 10 '13 at 19:27
  • Maybe if you keep a reference to the notification that a Foreground service MUST have, you can keep track that way? See this: http://stackoverflow.com/a/12660451/2684 (untested) – Martin Marconcini Dec 13 '13 at 01:21

3 Answers3

0

The only thing I can think of is checking whether the service's process importance indicates that it's running a foreground service or the foreground activity:

private boolean isForegroundOrForegroundService() {
    //Equivalent of RunningAppProcessInfo.IMPORTANCE_FOREGROUND_SERVICE on API 23
    //On prior versions to API 23, maybe the OS just uses 100 as foreground service importance?
    int IMPORTANCE_FOREGROUND_SERVICE = 125;
    return findThisProcess().importance <= IMPORTANCE_FOREGROUND_SERVICE;
}

private ActivityManager.RunningAppProcessInfo findThisProcess() {
    List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = activityManager.getRunningAppProcesses();
    for (ActivityManager.RunningAppProcessInfo proc : runningAppProcesses)
        if (proc.pid == Process.myPid())
            return proc;

    throw new RuntimeException("Couldn't find this process");
}

For this to work, there are a few constraints:

  • The service must be the only service in the process that tries to run in the foreground since otherwise you won't know which service caused the process to enter foreground mode.
  • There mustn't be any activities that run in the same process since having an activity open also causes the process to enter foreground mode.
  • Nothing else must be able to make the process enter foreground mode other than the service itself for the same reasons as above.

So you'll probably want to put the service in its own dedicated process. Unfortunately, this makes your app structure difficult since multi-process app development is a lot more complicated than single-process.

Note that this is mostly just theory; I haven't tested this much or used it in any real-world applications. Let me know how it goes if you pursue this approach.

Sam
  • 40,644
  • 36
  • 176
  • 219
-1

Try this code:

private boolean isActivityRunning() {
        List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(1);
        ComponentName runningActivity = tasks.get(0).topActivity;
        return runningActivity.getPackageName().startsWith("com.mypackage");
    }
-3

if I understood your question correctly and if we assume that foreground means that your application has some activity, you could declare global static variable in your application, e.g. boolean bIsForeground. On you activity you can set:

@Override
protected void onResume() {
    super.onResume();
    bIsForeground = true;
}


@Override
protected void onPause() {
    super.onResume();
    bIsForeground = false;
}

so every time your activity is foreground or "on screen" this variable should be true and this way your service can know is foreground active.

Moebius
  • 640
  • 4
  • 9