如何检查应用程序是否在 Android 上运行? [英] How can I check if an app running on Android?

查看:35
本文介绍了如何检查应用程序是否在 Android 上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名 Android 开发人员,我想在我的应用程序中编写一个 if 语句.在此语句中,我想检查默认浏览器(Android 操作系统中的浏览器)是否正在运行.我怎样才能以编程方式做到这一点?

I am an Android developer and I want to write an if statement in my application. In this statement I want to check if the default browser (browser in Android OS) is running. How can I do this programmatically?

推荐答案

添加以下 Helper 类:

Add the below Helper class:

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;
        }
    }

现在您可以通过以下代码检查您想要的应用程序是否正在运行:

Now you can check from the below code if your desired App is running or not:

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

这篇关于如何检查应用程序是否在 Android 上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆