Android - 通过 Facebook 分享自定义链接

标签 android facebook android-intent share

当用户打开我的应用程序时,我需要在我的应用程序中共享特定部分,如果他下载我的应用程序,它会直接导航到该部分(它可能是嵌套 fragment )。

String AppURL = "https://play.google.com/store/apps/details?id=" + getApplicationContext().getPackageName();
String urlToShare = "http://stackoverflow.com/questions/7545254"; // I need custom url to specific part in my app 

// See if official Facebook app is found
boolean facebookAppFound = false;
List<ResolveInfo> matches = 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));
}else{

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
// intent.putExtra(Intent.EXTRA_SUBJECT, "Foo bar"); // NB: has no effect!
intent.putExtra(Intent.EXTRA_TEXT, urlToShare);
}

startActivity(intent);

这是我需要解决的问题

enter image description here

最佳答案

为了更好地分享 Facebook 中的内容,请使用最新的 Facebook SDK。这将使您的任务更简单。因为Android Intent在我们使用share to facebook的时候有自己的限制。请参阅下面关于此的我的回答

Share text via Intent on Facebook without using Facebook sdk

您发布的屏幕截图似乎有来自应用程序的共享链接。

此处介绍如何将 Facebook SDK 集成到您的项目中。

Facebook SDK integration

然后使用下面的代码在facebook上分享链接。

ShareDialog shareDialog;
FacebookSdk.sdkInitialize(Activity.this);
shareDialog = new ShareDialog(act);
ShareLinkContent linkContent = new ShareLinkContent.Builder()
                    .setContentTitle("title")
                    .setContentDescription(
                            "Description")
                    .setContentUrl(Uri.parse("your url")).build();
            shareDialog.show(linkContent);

可以找到来自 Facebook 的更多共享选项 here ,这非常简单明了。

编码愉快..!!

关于Android - 通过 Facebook 分享自定义链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35155890/

相关文章:

android - 如何从 kotlin 中的类实例化对象

android - 在 webview android 中显示希伯来语内容

iphone - iOS Facebook 无法将源图像发布到墙上只能发布链接

node.js - Facebook 信使机器人错误 "TypeError: Cannot read property ' 0' of undefined."

android - 开始一个使用 asynctask 的新 Activity

android - 如何接收USB连接状态广播?

java - 如何使播放音频的同一按钮在单击按钮时停止?

android - Eclipse 安卓错误

android - 如何在 Android 中从 Facebook 检索我的位置和电话号码?

android - android 4.3中无法通过action_send Intent上传视频