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

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

    To filter on sytem based apps :

    private boolean isSystemPackage(ResolveInfo ri) {
        return (ri.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
    }
    
    0 讨论(0)
  • 2020-11-21 07:18

    @Jas: I don't have that code anymore, but I've found something close. I've made this to search for "components" of my application, they are just activities with a given category.

    private List<String> getInstalledComponentList() {
        Intent componentSearchIntent = new Intent();
        componentSearchIntent.addCategory(Constants.COMPONENTS_INTENT_CATEGORY);
        componentSearchIntent.setAction(Constants.COMPONENTS_INTENT_ACTION_DEFAULT);
        List<ResolveInfo> ril = getPackageManager().queryIntentActivities(componentSearchIntent, PackageManager.MATCH_DEFAULT_ONLY);
        List<String> componentList = new ArrayList<String>();
        Log.d(LOG_TAG, "Search for installed components found " + ril.size() + " matches.");
        for (ResolveInfo ri : ril) {
            if (ri.activityInfo != null) {
                componentList.add(ri.activityInfo.packageName);// + ri.activityInfo.name);
                Log.d(LOG_TAG, "Found installed: " + componentList.get(componentList.size()-1));
            }
        }
        return componentList;
    }
    

    I've commented the part where it gets the activity name, but it's pretty straightforward.

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

    Here a good example:

    class PInfo {
        private String appname = "";
        private String pname = "";
        private String versionName = "";
        private int versionCode = 0;
        private Drawable icon;
        private void prettyPrint() {
            Log.v(appname + "\t" + pname + "\t" + versionName + "\t" + versionCode);
        }
    }
    
    private ArrayList<PInfo> getPackages() {
        ArrayList<PInfo> apps = getInstalledApps(false); /* false = no system packages */
        final int max = apps.size();
        for (int i=0; i<max; i++) {
            apps.get(i).prettyPrint();
        }
        return apps;
    }
    
    private ArrayList<PInfo> getInstalledApps(boolean getSysPackages) {
        ArrayList<PInfo> res = new ArrayList<PInfo>();        
        List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
        for(int i=0;i<packs.size();i++) {
            PackageInfo p = packs.get(i);
            if ((!getSysPackages) && (p.versionName == null)) {
                continue ;
            }
            PInfo newInfo = new PInfo();
            newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
            newInfo.pname = p.packageName;
            newInfo.versionName = p.versionName;
            newInfo.versionCode = p.versionCode;
            newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
            res.add(newInfo);
        }
        return res; 
    }
    
    0 讨论(0)
  • 2020-11-21 07:20

    Here's a cleaner way using the PackageManager

    final PackageManager pm = getPackageManager();
    //get a list of installed apps.
    List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
    
    for (ApplicationInfo packageInfo : packages) {
        Log.d(TAG, "Installed package :" + packageInfo.packageName);
        Log.d(TAG, "Source dir : " + packageInfo.sourceDir);
        Log.d(TAG, "Launch Activity :" + pm.getLaunchIntentForPackage(packageInfo.packageName)); 
    }
    // the getLaunchIntentForPackage returns an intent that you can use with startActivity() 
    

    More info here http://qtcstation.com/2011/02/how-to-launch-another-app-from-your-app/

    0 讨论(0)
  • 2020-11-21 07:25
    private static boolean isThisASystemPackage(Context context, PackageInfo  packageInfo ) {
            try {
                PackageInfo sys = context.getPackageManager().getPackageInfo("android", PackageManager.GET_SIGNATURES);
                return (packageInfo != null && packageInfo.signatures != null &&
                        sys.signatures[0].equals(packageInfo.signatures[0]));
            } catch (NameNotFoundException e) {
                return false;
            }
        }
    
    0 讨论(0)
  • 2020-11-21 07:27

    Get All the apps:

        PackageManager pm = getContext().getPackageManager();
        List<ApplicationInfo> apps = pm.getInstalledApplications(0);
    

    Check if installed app then open:

    if((app.flags & (ApplicationInfo.FLAG_UPDATED_SYSTEM_APP | ApplicationInfo.FLAG_SYSTEM)) > 0) {
                    String app_package = app.packageName;
    Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(app_package);
    context.startActivity(launchIntent);
    
    0 讨论(0)
提交回复
热议问题