简体   繁体   English

Android:检查用户设备上是否存在Facebook应用

[英]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: 为了打开官方的Facebook应用程序,我使用以下网址:

fb://pages/PAGE_ID

instead of http://facebook.com/PAGE_ID 而不是http://facebook.com/PAGE_ID

Because in that case you get a list of browsers to open the url instead of teh facebook app. 因为在这种情况下你会得到一个浏览器列表来打开网址而不是teh facebook app。

It works if the user has facebook app installed. 如果用户安装了facebook应用程序,它将起作用。 However it crashes if the user doesn't have facebook app. 但是,如果用户没有Facebook应用程序,它会崩溃。

Is there any way to check if the user has the facebook app? 有什么方法可以检查用户是否有facebook应用程序?

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: 在原生Android应用程序中,这很容易实现:

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. 我想你可以重复使用我编写的代码来检查设备上是否安装了twitter应用程序,以检查是否安装了facebook应用程序。 For twitterApps list you have to replace the values by "com.facebook.katana". 对于twitterApps列表,您必须用“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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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