get Installed App Names - Android App

Android examples for App:APK Information

Description

get Installed App Names

Demo Code


//package com.java2s;
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);
                }/*from  w  ww.  j a va 2s  .  c o  m*/
            }
            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;
        // UNTESTED APPS BUT PROBABLY BAD IDEA TO RESTORE DATA
        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;
        // UNTESTED APPS BUT PROBABLY USELESS OR USER UNLIKELY TO RESTORE DATA
        if (appPackageName.startsWith("jackpal.androidterm")
                || appPackageName.startsWith("com.noshufou.android.su"))
            supported = false;
        return supported;
    }
}

Related Tutorials