Android 如何获取正在运行的应用程序列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3304685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:36:39  来源:igfitidea点击:

How to get the list of running applications?

android

提问by Jimmy

I am working on an app which needs the information of the apps running at the system up to now. Is there an API/method to retrieve that kind of information?

我正在开发一个应用程序,它需要到目前为止在系统上运行的应用程序的信息。是否有 API/方法来检索此类信息?

回答by desidigitalnomad

You cannotdetect an App launch in Android, but you can get the list of currently open apps and check if the app you're looking for is open or not using the following code:

无法在 Android 中检测应用程序启动,但您可以使用以下代码获取当前打开的应用程序列表并检查您要查找的应用程序是否已打开:

ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses();

for (int i = 0; i < runningAppProcessInfo.size(); i++) {
  if(runningAppProcessInfo.get(i).processName.equals("com.the.app.you.are.looking.for")) {
    // Do your stuff here.
  }
}

You can also check if the app is running in the foreground using this method

您还可以使用此方法检查应用程序是否在前台运行

public static boolean isForeground(Context ctx, String myPackage){
    ActivityManager manager = (ActivityManager) ctx.getSystemService(ACTIVITY_SERVICE);
    List< ActivityManager.RunningTaskInfo > runningTaskInfo = manager.getRunningTasks(1); 

    ComponentName componentInfo = runningTaskInfo.get(0).topActivity;
    if(componentInfo.getPackageName().equals(myPackage)) {
        return true;
    }       
    return false;
}

回答by hsyn tr

public static String getActiveApps(Context context) {

    PackageManager pm = context.getPackageManager();
    List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

    String value = u.dateStamp(); // basic date stamp
    value += "---------------------------------\n";
    value += "Active Apps\n";
    value += "=================================\n";

    for (ApplicationInfo packageInfo : packages) {

        //system apps! get out
        if (!isSTOPPED(packageInfo) && !isSYSTEM(packageInfo)) {

            value += getApplicationLabel(context, packageInfo.packageName) + "\n" + packageInfo.packageName  + "\n-----------------------\n";

        }
    }

    return value;

    //result on my emulator

    /* 2 Ekim 2017 Pazartesi 14:35:17
    ---------------------------------
    Active Apps
    =================================
    SystemSetting
    com.xyz.systemsetting
    -----------------------
    myMail
    com.my.mail
    -----------------------
    X-plore
    com.lonelycatgames.Xplore
    -----------------------
    Renotify
    com.liamlang.renotify
    -----------------------
    Mail Box
    com.mailbox.email
    -----------------------   */


}

some opened apps

一些打开的应用

isSTOPPED

已停止

private static boolean isSTOPPED(ApplicationInfo pkgInfo) {

    return ((pkgInfo.flags & ApplicationInfo.FLAG_STOPPED) != 0);
}

isSYSTEM

是系统

private static boolean isSYSTEM(ApplicationInfo pkgInfo) {

    return ((pkgInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
}

getApplicationLabel

获取应用程序标签

public static String getApplicationLabel(Context context, String packageName) {

    PackageManager        packageManager = context.getPackageManager();
    List<ApplicationInfo> packages       = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
    String                label          = null;

    for (int i = 0; i < packages.size(); i++) {

        ApplicationInfo temp = packages.get(i);

        if (temp.packageName.equals(packageName))
            label = packageManager.getApplicationLabel(temp).toString();
    }

    return label;
}

回答by David Underhill

You can get information about running processes using the ActivityManagerclass.

您可以使用ActivityManager类获取有关正在运行的进程的信息。