Android 判断app是否安装

1.

private boolean isAppInstalled(Context context, String uri) {  
        PackageManager pm = context.getPackageManager();  
        boolean installed = false;  
        try {  
            pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);  
            installed = true;  
        } catch (PackageManager.NameNotFoundException e) {  
            installed = false;  
        }  
        return installed;  
    }  

2.

public boolean isAppInstalled(Context context, String packageName) {  
        final PackageManager packageManager = context.getPackageManager();  
        List pinfo = packageManager.getInstalledPackages(0);  
        List pName = new ArrayList();  
        if (pinfo != null) {  
            for (int i = 0; i < pinfo.size(); i++) {  
                String pn = pinfo.get(i).packageName;  
                pName.add(pn);  
            }  
        }  
        return pName.contains(packageName);  
    }  

例子: 

                 if (isInstallApp(MainActivity.this, "com.xxx")) {  
                    System.out.println("Installed!!");  
                } else {  
                    System.out.println("Not installed!!");  
                }      

 

你可能感兴趣的:(移动开发)