【Android】Share via facebook with appName or without appName 有标签、无标签

来源:互联网 发布:mac pro 怎么弹出u盘 编辑:程序博客网 时间:2024/05/17 02:26

转载署源–By-KyleCe

————————————
1. 通过 Action_View

    private void shareFBViaActionView(Context context) {        String urlToShare = "https://www.numetriclabz.com/android-linkedin-integration-login-tutorial/";        Intent intent = new Intent(Intent.ACTION_SEND);        intent.setType("text/plain");        intent.putExtra(Intent.EXTRA_TEXT, urlToShare);// See if official Facebook app is found        boolean facebookAppFound = false;        List<ResolveInfo> matches = context.getPackageManager().queryIntentActivities(intent, 0);        for (ResolveInfo info : matches) {            if (info.activityInfo.packageName.toLowerCase().startsWith("com.facebook.katana")) {                intent.setPackage(info.activityInfo.packageName);                facebookAppFound = true;                break;            }        }// As fallback, launch sharer.php in a browser        if (!facebookAppFound) {        // 没有安装的情况,可以用文章中的第二种处理方式            String sharerUrl = "https://www.facebook.com/sharer/sharer.php?u=" + urlToShare;            intent = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl));        }        context.startActivity(intent);    }

如果已经安装facebook native应用,则会直接跳到facebook的分享界面,可以看到富文本框里的内容是不带AppName的:(图中最底部的一行)
这里写图片描述


2.如果分享出来的链接不遵循web的相应标准

facebook是抓取不到具体的标题、副标题、图片信息的,这种情况下,如果想设置这些信息,可以通过下面的方法尝试:(当然,你需要在app中加入facebook sdk,在manifest中加入相应Activity声明,这些在官网文档中都有说明,不赘述)

ShareDialog shareDialog = new ShareDialog(activityForResult);  // initialize facebook shareDialog.if (ShareDialog.canShow(ShareLinkContent.class)) {            String title = "Tips";            String description = "How to fix problem?";            String contentUrl = "http://pin.it/TLl3GcY";            ShareLinkContent linkContent = new ShareLinkContent.Builder()                    .setContentTitle(title)                    .setImageUrl(Uri.parse("https://www.numetriclabz.com/wp-content/uploads/2015/11/114.png"))                    .setContentDescription(description)                    .setContentUrl(Uri.parse(contentUrl))                    .build();            shareDialog.show(linkContent);  // Show facebook ShareDialog        }

值得注意的是,用这种dialog形式的分享框,在手机上已经安装facebook native 应用的情况下,是会直接跳转到facebook的,并且会带上你在申请appId时所填的appName;
如果没有安装,弹出dialog形式的分享时,则不会显示appName

PS:测试基于 小米 Mi3 + CM android 5.1.1 + Facebook SDK 4.8.0

0 0