我现在正在做一个想要拥有分享按钮(对话框)的小项目,当用户点击它时,它会自动登录到他/她的fb帐户并分享他们想要的内容。
在fb dev的教程之后,我的应用程序可以将内容共享到墙上,但需要在共享之前使用fblogin按钮登录。
我已经阅读了stackoverflow的帖子: Android - Facebook SDK 3 - How to login programmatically without LoginButton
更新: 我在项目中使用onActivityResult实现了feedDialog,我发现我可以使用一个按钮登录和共享。 HOWEVER ,当我重建应用程序/重新启动手机时,我必须按两次按钮才能在第一时间分享,但稍后会变为正常(按一次)。< / p>
P.S.I已使用 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();
}
答案 0 :(得分:15)
非常感谢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() {
//........................
}
答案 1 :(得分:11)
我认为这里的问题是,当您构建菜单时,检查活动会话是否为空,如果是,则调用openActiveSession,并等待onActivityResult被调用。
这一切都是正确的,但是,如果用户之前已经授权您的应用程序,那么访问令牌等都保存在令牌缓存中,并且调用openActiveSession将实际立即打开会话(不调用onActivityResult)。 / p>
真正正确的方法是将StatusCallback传递给您的公开调用(而不是null),并在该回调中,检查会话是否已打开,并调用您的publishFeedDialog方法。
答案 2 :(得分: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);
}
}
答案 3 :(得分:0)
LoginButton
只是一个从FB获取会话的工具。
如果您已经从FB获得会话,则需要检查发布权限。