How to get a list of installed android applications and pick one to run

后端 未结 19 1867
误落风尘
误落风尘 2020-11-21 06:26

I asked a similar question to this earlier this week but I\'m still not understanding how to get a list of all installed applications and then pick one to run.

I\'v

19条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-21 07:16

    If there are multiple launchers in a one package above code has a problem. Eg: on LG Optimus Facebook for LG, MySpace for LG, Twitter for LG contains in a one package name SNS and if you use above SNS will repeat. After hours of research I came with below code. Seems to work well.

    private List getInstalledComponentList()
                throws NameNotFoundException {
            final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            List ril = getPackageManager().queryIntentActivities(mainIntent, 0);
            List componentList = new ArrayList();
            String name = null;
    
            for (ResolveInfo ri : ril) {
                if (ri.activityInfo != null) {
                    Resources res = getPackageManager().getResourcesForApplication(ri.activityInfo.applicationInfo);
                    if (ri.activityInfo.labelRes != 0) {
                        name = res.getString(ri.activityInfo.labelRes);
                    } else {
                        name = ri.activityInfo.applicationInfo.loadLabel(
                                getPackageManager()).toString();
                    }
                    componentList.add(name);
                }
            }
            return componentList;
        }
    

提交回复
热议问题