如何查看在Android上运行的应用?

时间:2010-11-18 08:45:46

标签: java android browser if-statement

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

6 个答案:

答案 0 :(得分:126)

添加以下助手类:

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
}

答案 1 :(得分:0)

您可以通过以下方法检查

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

答案 2 :(得分:0)

isInBackground是应用程序的状态

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

答案 3 :(得分:0)

最佳解决方案是:

创建这样的界面

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

现在添加课程

公共类AppLifecycleHandler实现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

答案 4 :(得分: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
}

答案 5 :(得分:0)

如果您使用 Kotlin

   private fun isAppRunning(context: Context, packageName: String): Boolean {
        val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        activityManager.runningAppProcesses?.apply {
            for (processInfo in this) {
                if (processInfo.processName == packageName) {
                    return true
                }
            }
        }
        return false
    }