CrazyCoder
CrazyCoder

Reputation: 2605

Checking App is running?

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.

Upvotes: 2

Views: 602

Answers (2)

Thkru
Thkru

Reputation: 4199

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.

Upvotes: 0

Shankar Agarwal
Shankar Agarwal

Reputation: 34775

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

Upvotes: 2

Related Questions