欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android 判断手机是否安装某个应用

程序员文章站 2022-05-29 10:14:01
...

转载:https://blog.csdn.net/u010880009/article/details/53883396

判断手机是否安装某个应用的方法有很多,但是有的也存在问题,下面写一个项目里常用的,能用的:

    /**
     * 判断手机是否安装某个应用
     * @param context
     * @param appPackageName  应用包名
     * @return   true:安装,false:未安装
     */
    public static boolean isApplicationAvilible(Context context, String appPackageName) {
        PackageManager packageManager = context.getPackageManager();// 获取packagemanager
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (appPackageName.equals(pn)) {
                    return true;
                }
            }
        }
        return false;
    }

下面这个方法在使用的时候有些情况下会报错:java.lang.RuntimeException: Package manager has died

    /**
     * 判断手机是否安装某个应用
     * @param context
     * @param packageName  应用包名
     * @return   true:安装,false:未安装
     * 该方法容易报错:java.lang.RuntimeException: Package manager has died
     */
    public static boolean isAppInstallen(Context context ,String packageName){
        PackageManager pm = context.getPackageManager();
        boolean installed = false;
        try {
                pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
            installed = true;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            installed = false;
        }
        return  installed;
 
    }

 

相关标签: package Android