1

I have added one code to check if Facebook installed or not, in normal cases it is working but when facebook comes by default on some devices it is not working, it says package not found. can anyone help me here?

public Boolean checkFbInstalled() {
        PackageManager pm = getPackageManager();
        boolean flag = false;
        try {
            pm.getPackageInfo("com.facebook.katana", PackageManager.GET_ACTIVITIES);
            flag = true;
        } catch (PackageManager.NameNotFoundException e) {
            flag = false;
        }
        if (flag == false) {
            try {
                pm.getPackageInfo("com.facebook.lite", PackageManager.GET_ACTIVITIES);
                flag = true;
            } catch (PackageManager.NameNotFoundException e) {
                flag = false;
            }
        }

        if (flag == false) {
            try {
                pm.getPackageGids("com.facebook.katana");
                flag = true;
            } catch (PackageManager.NameNotFoundException e) {
                flag = false;
            }
        }
        return flag;
    }
ADM
  • 20,406
  • 11
  • 52
  • 83

1 Answers1

2

Check if the facebook package name exist with try-catch:

try{
    ApplicationInfo info = getPackageManager().
            getApplicationInfo("com.facebook.katana", 0 );
    return true;
}catch( PackageManager.NameNotFoundException e ){
    return false;
}

Note: If you like to see if the Facebook SDK exist and not the Facebook app you need to change the package name on the getApplicationInfo() method from com.facebook.katana to com.facebook.android

  • not working, normal situations this code is working, but when Facebook comes by default in mobile it is not working, it always gives namenotfoundexception only – safiya akhtar Mar 15 '21 at 17:14