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

后端 未结 19 1839
误落风尘
误落风尘 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:12

    you can use this :

    PackageManager pm = getApplicationContext().getPackageManager();
                    List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
                    for (final ResolveInfo app : activityList) 
                    {
                       if ((app.activityInfo.name).contains("facebook")) 
                       {
                         // facebook  
                       }
    
                       if ((app.activityInfo.name).contains("android.gm")) 
                       {
                         // gmail  
                       }
    
                       if ((app.activityInfo.name).contains("mms")) 
                       {
                         // android messaging app
                       }
    
                       if ((app.activityInfo.name).contains("com.android.bluetooth")) 
                       {
                         // android bluetooth  
                       }
                    }
    
    0 讨论(0)
  • 2020-11-21 07:12
    public static List<ApplicationInfo> getApplications(Context context) {
        return context.getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);
    }
    
    0 讨论(0)
  • 2020-11-21 07:12

    Since Android 11 (API level 30), most user-installed apps are not visible by default. You must either statically declare which apps and/or intent filters you are going to get info about in your manifest like this:

    <manifest>
        <queries>
            <!-- Explicit apps you know in advance about: -->
            <package android:name="com.example.this.app"/>
            <package android:name="com.example.this.other.app"/>
    
            <!-- Intent filter signatures that you are going to query: -->
            <intent>
                <action android:name="android.intent.action.SEND" />
                <data android:mimeType="image/jpeg" />
            </intent>
        </queries>
        
        ...
    </manifest>
    

    Or require the QUERY_ALL_PACKAGES permission.

    After doing the above, the other answers here still apply.

    Learn more here:

    • The migration documentation
    • The dedicated documentation page (has more information)
    • The official blog post
    0 讨论(0)
  • 2020-11-21 07:14

    Following is the code to get the list of activities/applications installed on Android :

    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    List<ResolveInfo> pkgAppsList = context.getPackageManager().queryIntentActivities( mainIntent, 0);
    

    You will get all the necessary data in the ResolveInfo to start a application. You can check ResolveInfo javadoc here.

    0 讨论(0)
  • 2020-11-21 07:14

    I had a requirement to filter out the system apps which user do not really use(eg. "com.qualcomm.service", "update services", etc). Ultimately I added another condition to filter down the app list. I just checked whether the app has 'launcher intent'.

    So, the resultant code looks like...

    PackageManager pm = getPackageManager();
            List<ApplicationInfo> apps = pm.getInstalledApplications(PackageManager.GET_GIDS);
    
            for (ApplicationInfo app : apps) {
                if(pm.getLaunchIntentForPackage(app.packageName) != null) {
                    // apps with launcher intent
                    if((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
                        // updated system apps
    
                    } else if ((app.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
                        // system apps
    
                    } else {
                        // user installed apps
    
                    }
                    appsList.add(app);
                }
    
            }
    
    0 讨论(0)
  • 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<String> getInstalledComponentList()
                throws NameNotFoundException {
            final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            List<ResolveInfo> ril = getPackageManager().queryIntentActivities(mainIntent, 0);
            List<String> componentList = new ArrayList<String>();
            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;
        }
    
    0 讨论(0)
提交回复
热议问题