我有应用程序检查应用程序是否已安装,并使用此布尔值

 final boolean b1 = appInstalledOrNot("com.whatsapp");

.....


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


我想将布尔值转换为字符串并获取正确的值。我尝试了此操作,但是如果要删除应用程序,则会收到错误消息“找不到包”

 String str = String.valueOf(b1);

最佳答案

   This is how you can modify your approach

            PackageInfo pinfo;

            try {
                pinfo = getPackageManager().getPackageInfo("Your package name", 0);
            }
            catch (NameNotFoundException e1) {
                pinfo = null;
            }

            if(pinfo!=null)
              {
               **"Installed"**
              }
            else
               {
               "Not Installed"
               }




我不明白您为什么要这样做,这将带来最佳结果。
检查一下。这就是我们将包与playstore中的包进行比较的方式。

public String getSignature(String pkgName) throws NameNotFoundException {
            PackageInfo pi = pkgMngr.getPackageInfo(pkgName, GET_SIGNATURES);
        String signature = pi.signatures[0].toCharsString();
        return signature;
    }

    public boolean doSignaturesMatch(String pkg1, String pkg2) {
        boolean match = pkgMngr.checkSignatures(pkg1, pkg2) == SIGNATURE_MATCH;
        return match;
    }

    public boolean isInstalledFromMarket(String pkgName)
            throws NameNotFoundException {
        String installerPkg = pkgMngr.getInstallerPackageName(pkgName);
        boolean installedFromMarket = "com.google.android.feedback"
                .equals(installerPkg);

10-08 19:02