Checking App is running?

陌路散爱 提交于 2019-12-12 19:53:05

问题


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.



来源:https://stackoverflow.com/questions/10284241/checking-app-is-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!