android - Facebook Android SDK 3.0,如何在没有登录按钮的情况下共享内容

标签 android facebook facebook-login facebook-android-sdk

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

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

我读了一篇来自 stackoverflow 的帖子: Android - Facebook SDK 3 - How to login programmatically without LoginButton

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

P.S.我已经用 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();
        }

最佳答案

非常感谢Ming Li的解决方案

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

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() {
            //........................
      }

关于android - Facebook Android SDK 3.0,如何在没有登录按钮的情况下共享内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14975224/

相关文章:

java - 为什么 SQLException 不捕获 SQLiteExcpetion?

java - 我在 cmd 上收到错误,即 build.xml 不存在

java - Android,如何模糊/玻璃/霜当前 Activity

swift - 在swift中设置facebook登录

javascript - 我如何获取来自 Facebook 的通知?

php - Facebook API 注销我的应用程序但不是 facebook

android - listView SetAdaptor 在应用程序启动时导致崩溃

javascript - 使用 FB.ui 时出现未知方法错误

facebook - 利用 Facebook 开放图元标签获取音乐播放列表的链接,以在 Facebook 新闻源中显示为播放列表

android - "com.parse.ParseRequest$ParseRequestException: Linking to an external account not supported yet with signup_or_login. Use update instead."