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

时间:2010-07-21 23:29:24

标签: android

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

3 个答案:

答案 0 :(得分:13)

无法检测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.
  }
}

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

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

答案 1 :(得分:9)

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

答案 2 :(得分:7)

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