简体   繁体   中英

Android: Check to see if Facebook app is present on user's device

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?

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

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);
    }
}

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;
    }

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