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?
我是一个Android开发人员,我想在我的应用程序中写一个if语句。在这个语句中,我想检查默认浏览器(Android OS中的浏览器)是否正在运行。我如何以编程的方式做这件事?
2 个解决方案
#1
108
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
}
#2
-1
You can check it by the following method
您可以使用以下方法进行检查
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
108
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
}
#2
-1
You can check it by the following method
您可以使用以下方法进行检查
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;
}