简体   繁体   中英

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.

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.

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. Did the same for Instagram as well, still doesn't work. I'm using DataBinding as you can see and I'm using it on a Fragment , not an Activity .

Would really appreciate help on this. Thanks for reading...

尝试使用另一个标志:

pm.getPackageInfo(uri, 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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