关闭

Android 判断app是否安装

41人阅读 评论(0) 收藏 举报
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<PackageInfo> pinfo = packageManager.getInstalledPackages(0);  
        List<String> pName = new ArrayList<String>();  
        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!!");  
                }  
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:10065次
    • 积分:362
    • 等级:
    • 排名:千里之外
    • 原创:18篇
    • 转载:58篇
    • 译文:0篇
    • 评论:0条