我是一名Android开发人员,我想if在我的应用程序中编写一条语句。在此语句中,我要检查默认浏览器(Android OS中
if
添加下面的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 }