简体   繁体   English

PackageManager无法检查已安装的应用程序

[英]PackageManager can't check for installed apps

I'm currently building an app that has Facebook and Instagram integration and I need to check whether the app has been installed on the user's device or not. 我目前正在构建一个具有FacebookInstagram集成的app ,我需要检查该app是否已安装在用户device

I've tried out : 我已经尝试了:

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

And I've passed in "com.instagram.android" and "com.facebook.katana" (Facebook) as the URI for the package name. 我已经传入"com.instagram.android""com.facebook.katana" (Facebook)作为package名称的URI。

My Code : 我的代码:

binding.facebook.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            boolean haveApp = appInstalledOrNot("com.facebook.katana");

            if (haveApp) {
                LoginManager.getInstance().logInWithReadPermissions(SocialMediaSharingFragment.this, Arrays.asList("public_profile", "email"));
            } else {
                Toast.makeText(getContext(), "App not installed", Toast.LENGTH_SHORT).show();
            }

        }
    });

I've deleted the Facebook App from my phone and ran the code, but somehow I'm still getting a true returned from the appInstalledOrNot method. 我已经从phone删除了Facebook App并运行了代码,但是不知何故我仍然从appInstalledOrNot方法返回了true Did the same for Instagram as well, still doesn't work. Instagram也执行相同操作,但仍然无效。 I'm using DataBinding as you can see and I'm using it on a Fragment , not an Activity . 如您所见,我正在使用DataBinding ,并且在Fragment而不是Activity上使用它。

Would really appreciate help on this. 非常感谢您的帮助。 Thanks for reading... 谢谢阅读...

尝试使用另一个标志:

pm.getPackageInfo(uri, 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM