How to get Category for each App on device on Android?

前端 未结 7 1925
隐瞒了意图╮
隐瞒了意图╮ 2020-12-03 04:25

I\'ve got an Android app which scans for all Apps installed on the device and then reports this to a server (it\'s an MDM agent). Any suggestions on how to get the Category

7条回答
  •  温柔的废话
    2020-12-03 04:27

    I know that this is an old post, but for anyone still looking for this, API level 26 (O) has added categories to android.content.pm.ApplicationInfo.

    From the docs https://developer.android.com/reference/android/content/pm/ApplicationInfo#category:

    public int category

    The category of this app. Categories are used to cluster multiple apps together into meaningful groups, such as when summarizing battery, network, or disk usage. Apps should only define this value when they fit well into one of the specific categories.

    Set from the R.attr.appCategory attribute in the manifest. If the manifest doesn't define a category, this value may have been provided by the installer via PackageManager#setApplicationCategoryHint(String, int). Value is CATEGORY_UNDEFINED, CATEGORY_GAME, CATEGORY_AUDIO, CATEGORY_VIDEO, CATEGORY_IMAGE, CATEGORY_SOCIAL, CATEGORY_NEWS, CATEGORY_MAPS, or CATEGORY_PRODUCTIVITY

    One can now do something like:

    PackageManager pm = context.getPackageManager();
    ApplicationInfo applicationInfo = pm.getApplicationInfo(packageName, 0);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        int appCategory = applicationInfo.category;
        String categoryTitle = (String) ApplicationInfo.getCategoryTitle(context, appCategory)
        // ...
    }
    

提交回复
热议问题