2

I've kept a button which takes user to my facebook page. In order that the official facebook app is opened I use the following url:

fb://pages/PAGE_ID

instead of http://facebook.com/PAGE_ID

Because in that case you get a list of browsers to open the url instead of teh facebook app.

It works if the user has facebook app installed. However it crashes if the user doesn't have facebook app.

Is there any way to check if the user has the facebook app?

Bilbo Baggins
  • 3,644
  • 8
  • 40
  • 64

3 Answers3

5

Have you already checked this? You can always check if an app is installed like this.

Community
  • 1
  • 1
gnclmorais
  • 4,897
  • 5
  • 30
  • 41
1

In an native android app, this is quite easy to achieve:

Uri dataUri = Uri.parse("fb://....");
Intent receiverIntent = new Intent(Intent.ACTION_VIEW, dataUri);

PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(receiverIntent, 0);

if (activities.size() > 0) {
    startActivity(receiverIntent);
} else {
    Uri webpage = Uri.parse("http://www.facebook.com/...");
    Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);

    packageManager = getPackageManager();
    activities = packageManager.queryIntentActivities(webIntent, 0);

    if (activities.size() > 0) {
        startActivity(webIntent);
    }
}
asgoth
  • 35,552
  • 12
  • 89
  • 98
1

I think you can reuse this code I wrote for checking if twitter app was installed on the device, to check if facebook app is installed. For twitterApps list you have to replace the values by "com.facebook.katana".

    public Intent findTwitterClient() {
            final String[] twitterApps = { "com.twitter.android", "com.handmark.tweetcaster", "com.seesmic", "com.thedeck.android", "com.levelup.touiteur", "com.thedeck.android.app" };

            Intent tweetIntent = new Intent(Intent.ACTION_SEND);
            tweetIntent.putExtra(Intent.EXTRA_TEXT, "#hashtagTest");
            tweetIntent.setType("text/plain");
            final PackageManager packageManager = getPackageManager();
            List<ResolveInfo> list = packageManager.queryIntentActivities(tweetIntent, PackageManager.MATCH_DEFAULT_ONLY);

            for (int i = 0; i < twitterApps.length; i++) {
                    for (ResolveInfo resolveInfo : list) {
                            String p = resolveInfo.activityInfo.packageName;
                            if (p != null && p.startsWith(twitterApps[i])) {
                                    tweetIntent.setPackage(p);
                                    return tweetIntent;
                            }
                    }
            }
            return null;
    }
Marc Cals
  • 2,963
  • 4
  • 30
  • 48