我现在正在做一个想要有一个分享按钮(对话框)的小项目,当用户点击它时,它会自动登录到他/她的 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();
}