安卓Facebook API和ShareLinkContent

12

我为我的Android游戏应用程序实现了一个按钮,允许用户分享游戏结果。

我已经集成了Facebook SDK,因此所有类都已知于我的项目中。 清单包含以下标记:

<meta-data android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/app_id" />

    <provider android:authorities="com.facebook.app.FacebookContentProvider16..."
        android:name="com.facebook.FacebookContentProvider"
        android:exported="true"/>
当我运行应用程序时,我可以使用以下代码分享游戏结果。
public void onShareResult(View view){
    FacebookSdk.sdkInitialize(getApplicationContext());
    callbackManager = CallbackManager.Factory.create();
    final ShareDialog shareDialog = new ShareDialog(this);

    shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {

        @Override
        public void onSuccess(Sharer.Result result) {
            Log.d(LOG_TAG, "success");
        }

        @Override
        public void onError(FacebookException error) {
            Log.d(LOG_TAG, "error");
        }

        @Override
        public void onCancel() {
            Log.d(LOG_TAG, "cancel");
        }
    });


    if (shareDialog.canShow(ShareLinkContent.class)) {

        ShareLinkContent linkContent = new ShareLinkContent.Builder()
               .setContentTitle("Game Result Highscore")
               .setContentDescription("My new highscore is " + sum.getText() + "!!")
               .setContentUrl(Uri.parse("https://play.google.com/store/apps/details?id=de.ginkoboy.flashcards"))

               //.setImageUrl(Uri.parse("android.resource://de.ginkoboy.flashcards/" + R.drawable.logo_flashcards_pro))
               .setImageUrl(Uri.parse("http://bagpiper-andy.de/bilder/dudelsack%20app.png"))
               .build();

        shareDialog.show(linkContent);
    }


}

然而,有一些事情我不理解。

  • 共享链接看起来与我在发布之前看到的不同。
  • 图像似乎可通过互联网获得。即无法从我的项目中设置资源的图像。

此外,我对Facebook要求的内容有些困惑。

这是Facebook如何显示我的帖子:

This is how Facebook displays my posting:

而这是我的应用程序似乎发布的内容:

And this is how my App seem to post the content

因此问题是:我的标题和描述去哪了?

最好的问候,

奥利弗


你可以查看这个使用Facebook SDK分享的例子:https://github.com/oliguo/android-facebook - msevgi
嘿@mustafasevgi, 感谢您提供这个信息。不过,似乎这个例子仅涉及从Facebook访问内容,而不是创建内容。 - Oliver Koehler
请检查我的答案。 - msevgi
4个回答

8

所以,我发现了为什么我的标题和描述在Facebook上不可见的原因。

首先感谢@mustafasevgi,但您的解决方案是针对SDK 3.5.x的,而我尝试使用的是SDK 4.0

回到解决方案......

我发现我已经将内容URL设置为我在Google Play商店中的应用程序。如果您将内容URL设置在Google Play商店之外,则标题和描述将不会被覆盖。


“content Url to my App inside the Google Play Store” 是什么意思? - Satyam
@Satyam: 我设置了内容标题、内容描述和内容网址 ShareLinkContent linkContent = new ShareLinkContent.Builder() .setContentTitle("游戏结果最高分") .setContentDescription("我的新最高分是 " + sum.getText() + "!!") .setContentUrl(Uri.parse("https://play.google.com/store/apps/details?id=de.ginkoboy.flashcards")) - Oliver Koehler
上面的代码片段在回复中和问题中是一样的吗?我没有找到任何关于这个问题的答案。你能帮我吗? - iappmaker
请问您能否解释一下“如果您在Google Play商店之外设置了内容URL”的含义? - iappmaker
3
如果您设置的URL指向"https://play.google.com/store/apps...",则标题和说明将直接从目标页面中获取。 如果您设置的URL指向Google Play商店之外的任何URL,则将考虑在ShareLinkContent.Builder()中输入的标题和说明。 因此,这不能通过更改代码来解决,而是要更改URL。 - Oliver Koehler

2
您可以使用ShareContent.Builder的setQuote("任何描述字符串")方法来设置要显示的链接引用。就像这样。
if (shareDialog.canShow(ShareLinkContent.class)) {

    ShareLinkContent linkContent = new ShareLinkContent.Builder()
           .setQuote("My new highscore is " + sum.getText() + "!!")
          .setContentUrl(Uri.parse("https://play.google.com/store/apps/details?id=de.ginkoboy.flashcards"))
           .build();

    shareDialog.show(linkContent);
}

附注:最新的Facebook SDK中,setTitle、setDescription和setImageUrl方法已经被弃用。


0

使用此代码可以与Facebook SDK 3.X一起使用。您可以使用WebDialog。

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.facebook.FacebookException;
import com.facebook.FacebookOperationCanceledException;
import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.model.GraphUser;
import com.facebook.widget.WebDialog;
import com.facebook.widget.WebDialog.OnCompleteListener;

public class FaceShare extends Activity {

   String link = "", id = "", pic = "", title = "";

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      Intent i = getIntent();
      Bundle b = i.getExtras();

      // link = b.getString("link");
      // id = b.getString("id");
      // pic = b.getString("pic");
      // title = b.getString("title");

      try {

         // start Facebook Login
         Session.openActiveSession(this, true, new Session.StatusCallback() {

            @Override
            public void call(Session session, SessionState state, Exception exception) {
               if (session.isOpened()) {

                  // make request to the /me API
                  Request.newMeRequest(session, new Request.GraphUserCallback() {

                     @Override
                     public void onCompleted(GraphUser user, Response response) {

                     }

                     // callback after Graph API response with user object
                  }).executeAsync();

                  publishFeedDialog(title, "title", "caption", link, pic);
               }
            }

            // callback when session changes state
         });
      }
      catch (Exception e) {
         Log.e("FACE", e.toString());
         finish();
      }
   }

   @Override
   public void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
   }

   private void publishFeedDialog(String name, String caption, String description, String link, String urlPicture) {
      Bundle params = new Bundle();
      // params.putString("name", name);
      // params.putString("caption", caption);
      // params.putString("description", description);
      // params.putString("link", link);
      // params.putString("picture", urlPicture);
      params.putString("name", "name");
      params.putString("caption", "caption");
      params.putString("description", "description");
      params.putString("link", "https://s-media-cache-ak0.pinimg.com/236x/1b/2b/19/1b2b19519b1b3439f783181026d9872b.jpg");
      params.putString("picture", "https://s-media-cache-ak0.pinimg.com/236x/1b/2b/19/1b2b19519b1b3439f783181026d9872b.jpg");

      Session session = Session.getActiveSession();

      WebDialog feedDialog = (new WebDialog.FeedDialogBuilder(FaceShare.this, session, params)).setOnCompleteListener(new OnCompleteListener() {
         public void onComplete(Bundle values, FacebookException error) {
            if (error == null) {
               // When the story is posted, echo the success
               // and the post Id.
               final String postId = values.getString("post_id");
               if (postId != null) {
                  Toast.makeText(getApplicationContext(), "Shared.", Toast.LENGTH_LONG).show();
                  finish();
               }
               else {
                  finish();
               }
            }
            else if (error instanceof FacebookOperationCanceledException) {
               finish();
            }
            else {
               Toast.makeText(getApplicationContext(),
                              "error occured, try again",
                              Toast.LENGTH_LONG).show();
               finish();
            }
         }

      })
                                                                                               .build();
      feedDialog.show();
   }

}

0
我使用了WebDialog系统,但是出现了以下信息:

当AndroidManifest.xml中没有声明com.facebook.LoginActivity为活动时,无法使用SessionLoginBehavior SSO_WITH_FALLBACK

有人知道为什么吗?


你在 AndroidManifest 中声明了这个 Activity 吗?类似于<activity android:name="com.facebook.LoginActivity"... - Oliver Koehler
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Oliver Koehler

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接