69

I am modifying my app to be able to catch if a user tries to publish without having the facebook app installed (required for SSO). Here is the code I am using:

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

The problem is, it is always catching an error. According to the question here, I need to request the appropriate permission but I don't know what permissions I need to request.

Is my problem a permission one or something else?

Community
  • 1
  • 1
easycheese
  • 5,859
  • 10
  • 53
  • 87

9 Answers9

105

com.facebook.android is the package name for the Facebook SDK. The Facebook app's package is com.facebook.katana.

Floern
  • 33,559
  • 24
  • 104
  • 119
Torid
  • 4,176
  • 1
  • 28
  • 29
  • 1
    if you're interested in a convenient way to check the package names of other apps on your device, I found this app helpful: https://play.google.com/store/apps/details?id=com.electricsheep.asi&hl=en_GB – hmac Feb 21 '17 at 16:15
  • @hmac the link doesn't work anymore – radus14 Jun 27 '23 at 07:05
12

To check whether or not an app is installed on Android use this method:

public static boolean isPackageInstalled(Context c, String targetPackage) {
    PackageManager pm = c.getPackageManager();
    try {
        PackageInfo info = pm.getPackageInfo(targetPackage, PackageManager.GET_META_DATA);
    } catch (NameNotFoundException e) {
        return false;
    }
    return true;
}

In your case use any of these packages:

  • com.facebook.orca
  • com.facebook.katana
  • com.example.facebook
  • com.facebook.android
boolean hasPackage = isPackageInstalled(MainActivity.this, "com.facebook.katana");
  • For Kotlin

      fun isPackageInstalled(packageName: String, context: Context): Boolean {
         return try {
                  val packageManager = context.packageManager
                  packageManager.getPackageInfo(packageName, 0)
                  true
              } catch (e: PackageManager.NameNotFoundException) {
                  false
              }
          }
    
Nishant Rajput
  • 2,053
  • 16
  • 28
6

You can check it for all Facebook Apps that any of Facebook apps are installed or not . For supporting OS level 11 we need to add this in AndrodiManifest.xml to avoid package name not found exception -

<manifest ...
<queries>
    <package android:name="com.facebook.katana" />
    <package android:name="com.facebook.lite" />
    <package android:name="com.facebook.android" />
    <package android:name="com.example.facebook" />
</queries>
 <application .....

Then add this method to you code -

public static String isFacebookAppInstalled(Context context){

        if(context!=null) {
            PackageManager pm=context.getPackageManager();
            ApplicationInfo applicationInfo;

            //First check that if the main app of facebook is installed or not
            try {
                applicationInfo = pm.getApplicationInfo("com.facebook.katana", 0);
                return applicationInfo.enabled?"com.facebook.katana":"";
            } catch (Exception ignored) {
            }

            //Then check that if the facebook lite is installed or not
            try {
                applicationInfo = pm.getApplicationInfo("com.facebook.lite", 0);
                return applicationInfo.enabled?"com.facebook.lite":"";
            } catch (Exception ignored) {
            }

            //Then check the other facebook app using different package name is installed or not
            try {
                applicationInfo = pm.getApplicationInfo("com.facebook.android", 0);
                return applicationInfo.enabled?"com.facebook.android":"";
            } catch (Exception ignored) {
            }

            try {
                applicationInfo = pm.getApplicationInfo("com.example.facebook", 0);
                return applicationInfo.enabled?"com.example.facebook":"";
            } catch (Exception ignored) {
            }
        }
        return "";
    }

And then launch the app -

if (!TextUtils.isEmpty(isFacebookAppInstalled(context))) {
 /* Facebook App is installed,So launch it. 
  It will return you installed facebook app's package
  name which will be useful to launch the app */

  Uri uri = Uri.parse("fb://facewebmodal/f?href=" + yourURL);
 Intent intent = context.getPackageManager().getLaunchIntentForPackage(isFacebookAppInstalled(context);
                if (intent != null) {
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.setData(uri);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                }
                else {
                    Intent intentForOtherApp = new Intent(Intent.ACTION_VIEW, uri);
                    context.startActivity(intentForOtherApp);
                } 
 }
Gk Mohammad Emon
  • 6,084
  • 3
  • 42
  • 42
4
 if (isAppInstalled()) {
        Toast.makeText(getApplicationContext(), "facebook app already installed", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(getApplicationContext(), "facebook app not installing", Toast.LENGTH_SHORT).show();
    }



public boolean isAppInstalled() {
            try {
                getApplicationContext().getPackageManager().getApplicationInfo("com.facebook.katana", 0);
                return true;
            } catch (PackageManager.NameNotFoundException e) {
                return false;
            }
        }
Sanjay Mangaroliya
  • 4,286
  • 2
  • 29
  • 33
1

Write the function in Utilities or anywhere suit for you.This will function will help you to check any app installed or not.let me say for myself it is in Utilities.java

public static boolean isAppInstalled(Context context, String packageName) {
        try {
            context.getPackageManager().getApplicationInfo(packageName, 0);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }

Then, Call this function from anywhere. for eg to check facebook app

if(Utilities.isAppInstalled(getApplicationContext(), "com.facebook.katana")) {
                    // Do something
                }else {
                    Intent i = new Intent(android.content.Intent.ACTION_VIEW);
                    i.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.facebook.katana"));
                    startActivity(i);
                }

Enjoy

yubaraj poudel
  • 3,821
  • 1
  • 31
  • 28
0

Best Approach is to pick the package name including com.facebook but anyway you may use following packages:

  • com.facebook.orca
  • com.facebook.katana
  • com.example.facebook
  • com.facebook.android
Abdul Rahman Majeed
  • 1,125
  • 2
  • 10
  • 22
-1
Intent i = new Intent(android.content.Intent.ACTION_VIEW);
i.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.facebook.katana"));
startActivity(i);

this code worked for me

Christian Phillips
  • 18,399
  • 8
  • 53
  • 82
Avinash
  • 70
  • 6
-1
if (isAppInstalled()) {
        Toast.makeText(getApplicationContext(), "facebook app already installed", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(getApplicationContext(), "facebook app not installing", Toast.LENGTH_SHORT).show();
    }

public boolean isAppInstalled() {
            try {
                getApplicationContext().getPackageManager().getApplicationInfo("com.facebook.katana", 0);
                return true;
            } catch (PackageManager.NameNotFoundException e) {
                return false;
            }
Bink
  • 1,934
  • 1
  • 25
  • 41
-1

myWebView.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.e("tag","url override url  = "+ url);

            if( url.startsWith("http:") || url.startsWith("https:") ) {
                return false;
            }

            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity( intent );

            return true;
        }





    });