Android Facebook API和ShareLinkContent

时间:2015-04-10 05:33:56

标签: android facebook

对于我的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

所以问题是:我的标题和描述在哪里消失了?

祝你好运

奥利弗

4 个答案:

答案 0 :(得分:7)

所以,我已经找到了为什么我的标题和描述在facebook中不可见的原因。

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

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

我发现我已将我的内容网址设置到Google Play商店内的应用程序中。如果您在Google Play商店之外设置了内容网址,则不会覆盖标题和说明。

答案 1 :(得分: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();
   }

}

答案 2 :(得分:0)

我使用过WebDialog系统,但我有这样的信息:

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

有谁知道为什么?

答案 3 :(得分:0)

您可以使用ShareContent.Builder的setQuote(&#34;任何描述字符串&#34;)方法来设置要为您的链接显示的引号。 像这样的东西

__set()

P.S。现在最新的Facebook SDK

中不推荐使用setTitle,setDescription和setImageUrl方法