简体   繁体   English

获取Android设备上已安装的应用程序数量?

[英]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. 我正在制作一个Android启动器,作为自己制作Android应用程序的介绍,而我的部分设计要求我知道用户设备上安装了多少应用程序,并且最好仅统计那些可以启动的普通应用程序。 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 . 您可以使用PackageManagergetInstalledApplications()方法。

From the documentation : 文档中

Return a List of all application packages that are installed on the device. 返回设备上安装的所有应用程序包的List 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. 如果设置了标志GET_UNINSTALLED_PACKAGES ,将返回所有应用程序的列表,包括使用DONT_DELETE_DATA删除的应用程序(带有数据目录的部分安装的应用程序)。

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♦ 正如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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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