如何检查应用程序是否在Android上运行?


70

我是一名Android开发人员,我想if在我的应用程序中编写一条语句。在此语句中,我要检查默认浏览器(Android OS中的浏览器)是否正在运行。如何以编程方式执行此操作?

Answers:


147

添加下面的Helper类:

public class Helper {

        public static boolean isAppRunning(final Context context, final String packageName) {
            final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            final List<ActivityManager.RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
            if (procInfos != null)
            {
                for (final ActivityManager.RunningAppProcessInfo processInfo : procInfos) {
                    if (processInfo.processName.equals(packageName)) {
                        return true;
                    }
                }
            }
            return false;
        }
    }

现在,您可以从以下代码中检查所需的应用程序是否正在运行:

if (Helper.isAppRunning(YourActivity.this, "com.your.desired.app")) {
    // App is running
} else {
    // App is not running
}

3
谢谢,但正如我每次看到com.android.browser运行时一样。当它是前景时,通常在列表的第3或第4位,否则它仍在列表中。因此,此语句每次都返回true。我怎么解决这个问题?仅当最近的进程是浏览器时,它才必须写“浏览器正在运行”。
sjor

1
在RunningAppProcessInfo场“LRU”会给您提供更多的参考应用程序运行时的相关信息查询文档页面- developer.android.com/reference/android/app/...
dhaval

有关更多详细信息,您可以在应用程序中维护一个计时器。在我过去的一个应用程序中,像gmail聊天状态一样显示进程状态,我们维护了应用程序时间以及重要性值
dhaval 2010年

1
@dhaval是否有任何方法可以检查应用程序是否已进入后台(即用户不再可见)?
SMR 2014年

有什么方法可以检查进程是否暂停或恢复?
毫不客气

2

isInBackground 是应用程序的状态

ActivityManager.RunningAppProcessInfo myProcess = new ActivityManager.RunningAppProcessInfo();
ActivityManager.getMyMemoryState(myProcess);
Boolean isInBackground = myProcess.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;

1

您可以通过以下方法进行检查

public static boolean isRunning(Context ctx) {
    ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);

    List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);

    for (ActivityManager.RunningTaskInfo task : tasks) {
        if (ctx.getPackageName().equalsIgnoreCase(task.baseActivity.getPackageName()))
            return true;
    }
    return false;
}

1
在kitkat之上工作吗?如果是的话,请提供代码。.谢谢
Rohit Heera

它已被弃用
Mahmoud Mabrok

1

最好的解决方案是:

创建一个这样的界面。

interface LifeCycleDelegate {
    void onAppBackgrounded();
    void onAppForegrounded();
}

现在添加一个处理所有活动生命周期回调的类。

public class AppLifecycleHandler implements 
 Application.ActivityLifecycleCallbacks, ComponentCallbacks2 {

LifeCycleDelegate lifeCycleDelegate;
boolean appInForeground = false;

public AppLifecycleHandler(LifeCycleDelegate lifeCycleDelegate) {
    this.lifeCycleDelegate = lifeCycleDelegate;
}

@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

}

@Override
public void onActivityStarted(Activity activity) {

}

@Override
public void onActivityResumed(Activity activity) {
    if (!appInForeground) {
        appInForeground = true;
        lifeCycleDelegate.onAppForegrounded();
    }
}

@Override
public void onActivityPaused(Activity activity) {

}

@Override
public void onActivityStopped(Activity activity) {

}

@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

}

@Override
public void onActivityDestroyed(Activity activity) {

}

@Override
public void onTrimMemory(int level) {
    if (level == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
        // lifecycleDelegate instance was passed in on the constructor
        appInForeground = false;
        lifeCycleDelegate.onAppBackgrounded();
    }
}

@Override
public void onConfigurationChanged(Configuration newConfig) {

}

@Override
public void onLowMemory() {

}
}

现在在扩展应用程序的类中

public class MyApplication extends Application implements 
 LifeCycleDelegate{
 @Override
public void onCreate() {
    super.onCreate();

    AppLifecycleHandler lifeCycleHandler = new 
    AppLifecycleHandler(MyApplication.this);
    registerLifecycleHandler(lifeCycleHandler);
}

@Override
public void onAppBackgrounded() {
    Log.d("Awww", "App in background");
}

@Override
public void onAppForegrounded() {
    Log.d("Yeeey", "App in foreground");
}

private void registerLifecycleHandler(AppLifecycleHandler lifeCycleHandler) {
    registerActivityLifecycleCallbacks(lifeCycleHandler);
    registerComponentCallbacks(lifeCycleHandler);
}
}

有关更多信息,请参见:https : //android.jlelse.eu/how-to-detect-android-application-open-and-close-background-and-foreground-events-1b4713784b57


他在谈论默认浏览器,而不是他自己的应用!
贾加尔

0
fun Context.isAppInForeground(): Boolean {

val application = this.applicationContext
val activityManager = this.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
val runningProcessList = activityManager.runningAppProcesses

  if (runningProcessList != null) {
     val myApp = runningProcessList.find { it.processName == application.packageName }
     ActivityManager.getMyMemoryState(myApp)
     return myApp?.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
  }

  return false
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.