简体   繁体   中英

Get number of installed apps on an Android device?

I'm making an Android launcher as an introduction to making Android apps for myself, and part of my design requires me to know how many apps are installed on a user's device, and preferably count only the ones which are normal apps that can be launched. I wish to store this number of apps in a global, integer variable. With only this goal in mind, what is the simplest way of just retrieving this number as that variable?

You could use the getInstalledApplications() method of PackageManager .

From the documentation :

Return a List of all application packages that are installed on the device. If flag GET_UNINSTALLED_PACKAGES has been set, a list of all applications including those deleted with DONT_DELETE_DATA (partially installed apps with data directory) will be returned.

Something like this should work:

int numberOfInstalledApps = getPackageManager(0).getInstalledApplications().size();

To filter out the system apps you could do something like this:

int numberOfNonSystemApps = 0;

List<ApplicationInfo> appList = getPackageManager().getInstalledApplications(0);
for(ApplicationInfo info : appList) {
    if((info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
        numberOfNonSystemApps++;
    }
}

The all non-system apps have a launch Intent so you just need to fetch the list of all apps and check, how many of them has a launch intent or if not then that app will be a system app.The list of all apps can easily be retrieved by package manager and then we go through the information of all apps while looking for the available launch intent.

As suggested by Darshan Patel : @Brad Larson♦

PackageManager pm = getPackageManager();
int nonSysAppsCount=0;
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for(ApplicationInfo packageInfo:packages){
    if( pm.getLaunchIntentForPackage(packageInfo.packageName) != null ){
                String currAppName = pm.getApplicationLabel(packageInfo).toString();
           nonSysAppsCount++;
               //This app is a non-system app
    }
    else{
        //System App
    }
}

If you are looking for the non-system applications installed on a given device, you can do the following :

public static ArrayList<String> getInstalledApps() {
    ArrayList<String> appList = new ArrayList<>();
    List<PackageInfo> packList = getPackageManager().getInstalledPackages(0);
    for (int i=0; i < packList.size(); i++) {
        PackageInfo packInfo = packList.get(i);
        if (  (packInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
            String appName = packInfo.applicationInfo.loadLabel(getPackageManager()).toString();
            appList.add(appName);
            Log.e("App >" + Integer.toString(i), appName);
        }
    }
    return appList;
}

So, you can the list by doing :

int number = getInstalledApps().size();

Additionally, you can start any of the applications by calling :

Intent myIntent = new Intent(Intent.ACTION_MAIN, null);
myIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List appsList = context.getPackageManager().queryIntentActivities(myIntent, 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM