安卓(Android) 怎么获取已安装的应用程序名称

2019年10月17日21:34:23安卓(Android) 怎么获取已安装的应用程序名称已关闭评论 213 1868字阅读6分13秒

1.简介

安卓(Android) 怎么获取已安装的应用程序名称?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */


import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;

public class Main {
    private static Activity mContext;

    public static List<String> getInstalledAppNames(
            boolean onlySupportedApps) {
        List<ApplicationInfo> ai = getInstalledApps();
        if (ai != null) {
            List<String> packageNames = new ArrayList<String>();
            for (ApplicationInfo pi : ai) {
                if (!onlySupportedApps
                        || isBackupSupportedForApp(pi.packageName)) {
                    packageNames.add(pi.packageName);
                }
            }
            return packageNames;
        } else {
            return null;
        }
    }

    public static List<ApplicationInfo> getInstalledApps() {
        if (mContext != null) {
            final PackageManager pm = mContext.getPackageManager();
            return pm
                    .getInstalledApplications(PackageManager.GET_META_DATA);
        } else
            return null;
    }

    public static List<ApplicationInfo> getInstalledApps(
            boolean onlySupportedApps) {
        if (mContext != null) {
            List<ApplicationInfo> ai = getInstalledApps();
            if (!onlySupportedApps)
                return ai;

            List<ApplicationInfo> res = new ArrayList<ApplicationInfo>();
            for (ApplicationInfo pi : ai) {
                if (isBackupSupportedForApp(pi.packageName)) {
                    res.add(pi);
                }
            }

            return res;
        } else
            return null;
    }

    public static boolean isBackupSupportedForApp(String appPackageName) {
        boolean supported = true;
        if (appPackageName == null)
            return false;
        
        if (appPackageName.startsWith("com.android.")
                || appPackageName.startsWith("com.google.")
                || appPackageName.startsWith("android"))
            supported = false;
        if (appPackageName.startsWith("com.bel.android.")
                || appPackageName.startsWith("com.cyanogenmod.")
                || appPackageName.startsWith("com.koushikdutta."))
            supported = false;
        if (appPackageName.startsWith("com.swype.")
                || appPackageName.startsWith("com.svox.")
                || appPackageName.startsWith("com.tmobile.theme"))
            supported = false;
        
        if (appPackageName.startsWith("jackpal.androidterm")
                || appPackageName.startsWith("com.noshufou.android.su"))
            supported = false;
        return supported;
    }
}

 
继续阅读
编程十万个怎么办