Checking App is running?

2019-07-12 02:42发布

问题:

Is it possible for a service to check the following

  1. Whether a App is running or not?
  2. If yes, is it App running is background or foreground? Please suggest some way to do it.

回答1:

You need to use getRunningTasks to know whether the app is running or not.

From the docs:

public List getRunningTasks (int maxNum)

Return a list of the tasks that are currently running, with the most recent being first and older ones after in order.

For this to work you should include this in your AndroidManifest.xml

<uses-permission android:name="android.permission.GET_TASKS" />

And regarding App running background/foreground refer this LINK



回答2:

List<RunningAppProcessInfo> apps = ((ActivityManager) _context.getSystemService(Context.ACTIVITY_SERVICE)).getRunningAppProcesses();

...that's the way u get every running app, just use "apps.get(i).processName" or sth to get the processname u can check for.

And use <uses-permission android:name="android.permission.GET_TASKS" /> in your manifest, to make sure u have the right permission.