列出正在运行的应用程序总是重复相同的列表

时间:2018-01-21 15:35:27

标签: android

我已经测试this solution列出所有正在运行的应用,并且总是重复相同的列表(即使设备重新启动)。似乎存在一个必须在每次执行此代码之前擦除的历史记录,以获取实际运行的新应用程序列表。

然后我想知道是否存在一些能够消除这个假设历史的解决方案?

在否定的情况下,还有什么方法可以解决这个问题?

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

0 个答案:

没有答案