11

我现在正在做一个想要有一个分享按钮(对话框)的小项目,当用户点击它时,它会自动登录到他/她的 fb 帐户并分享他们想要的内容。

在 fb dev 的教程之后,我的应用程序可以将内容分享到墙上,但在分享之前需要使用 fblogin 按钮登录。

我已经阅读了来自 stackoverflow 的帖子: Android - Facebook SDK 3 - 如何在没有 LoginButton 的情况下以编程方式登录

更新:我已经在我的项目中实现了带有 onActivityResult 的 feedDialog,我发现我可以通过一个按钮登录和分享。 但是,当我重建应用程序/重新启动手机时,我必须按两次按钮才能第一次分享,但稍后恢复正常(按一次)。

PSI 已使用shareActionProvider实现它

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getSupportMenuInflater().inflate(R.menu.content_main, menu);
    /** Getting the actionprovider associated with the menu item whose id is share */
    mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share).getActionProvider();

    /** Getting the target intent */
    Intent intent = getDefaultShareIntent();

    /** Setting a share intent */       
    if(intent!=null){
        mShareActionProvider.setShareIntent(intent);
        mShareActionProvider.setOnShareTargetSelectedListener(new OnShareTargetSelectedListener(){
            @Override
            public boolean onShareTargetSelected(ShareActionProvider source, Intent intent) {
                if ("com.facebook.katana".equals(intent.getComponent().getPackageName())){
                    if (Session.getActiveSession() == null || Session.getActiveSession().isClosed()) {
                        Session.openActiveSession(Content.this, true, null);
                }else{
                        publishFeedDialog();
                    }
                    return true;
                }
                return false;
            }
        });
    }

    return super.onCreateOptionsMenu(menu);
}
@Override
   public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
        if (Session.getActiveSession() != null || Session.getActiveSession().isOpened())
            publishFeedDialog();
    }
private void publishFeedDialog() {
        Bundle params = new Bundle();
        params.putString("name", "ab");
        params.putString("caption", "cd");
        params.putString("description", "def");
        params.putString("link", "https://developers.facebook.com/android");
        params.putString("picture", "abc.jpg");

        WebDialog feedDialog = (
            new WebDialog.FeedDialogBuilder(Content.this,
                Session.getActiveSession(),
                params))
            .setOnCompleteListener(new OnCompleteListener() {

                @Override
                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(Content.this,
                                "Posted story, id: "+postId,
                                Toast.LENGTH_SHORT).show();
                        } else {
                            // User clicked the Cancel button
                            Toast.makeText(Content.this.getApplicationContext(), 
                                "Publish cancelled", 
                                Toast.LENGTH_SHORT).show();
                        }
                    } else if (error instanceof FacebookOperationCanceledException) {
                        // User clicked the "x" button
                        Toast.makeText(Content.this.getApplicationContext(), 
                            "Publish cancelled", 
                            Toast.LENGTH_SHORT).show();
                    } else {
                        // Generic, ex: network error
                        Toast.makeText(Content.this.getApplicationContext(), 
                            "Error posting story", 
                            Toast.LENGTH_SHORT).show();
                    }
                }

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

4 回答 4

15

非常感谢李明的解决方案

我终于得到答案了!!!!下面是代码,希望对其他开发者有所帮助

private Session.StatusCallback callback = new Session.StatusCallback() {
          @Override
          public void call(Session session, SessionState state,
            Exception exception) {
           onSessionStateChange(session, state, exception);
          }
    };
    private void onSessionStateChange(Session session, SessionState state, Exception exception) {
        if (state.isOpened()) {
            publishFeedDialog();
        }
    }
    //........................
                    if (Session.getActiveSession() == null || Session.getActiveSession().isClosed()) {
                            Session.openActiveSession(Content.this, true, callback);
                    }else{
                            publishFeedDialog();
                        }
     //.......................
    @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() {
            //........................
      }
于 2013-02-22T18:55:20.463 回答
11

我认为这里的问题是,当您构建菜单时,您会检查活动会话是否为空,如果是,则调用 openActiveSession,并等待调用 onActivityResult。

这都是正确的,但是,如果用户之前已经授权了您的应用程序,那么访问令牌等都保存在令牌缓存中,调用 openActiveSession 实际上会立即打开会话(无需调用 onActivityResult)。

真正正确的方法是将 StatusCallback 传递给您的打开调用(而不是 null),并在该回调中检查会话是否打开,然后调用您的 publishFeedDialog 方法。

于 2013-02-22T17:54:15.770 回答
4

Getting Started教程中对此进行了介绍:

    package com.firstandroidapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.TextView;
import com.facebook.*;
import com.facebook.model.*;

public class MainActivity extends Activity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

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

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

        // callback after Graph API response with user object
        @Override
        public void onCompleted(GraphUser user, Response response) {
          if (user != null) {
            TextView welcome = (TextView) findViewById(R.id.welcome);
            welcome.setText("Hello " + user.getName() + "!");
          }
        }
      });
    }


     }
    });
  }

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

}
于 2013-04-14T16:37:38.047 回答
0

LoginButton在 facebook-sdk-3.0 中只是一个从 FB 获取会话的工具。

如果您已经从 FB 获得会话,则需要检查发布权限。

于 2013-02-20T08:45:34.303 回答