首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >android facebook应用中断facebook sdk

android facebook应用中断facebook sdk
EN

Stack Overflow用户
提问于 2011-04-26 14:48:35
回答 1查看 1K关注 0票数 0

这几次我都在尝试使用facebook android sdk。

顺便说一句,我正在使用facebook到android的集成教程,它弹出对话框布局……链接http://integratingstuff.com/2010/10/14/integrating-facebook-into-an-android-application/

这是我使用的代码:

代码语言:javascript
复制
public class Sharefb extends Activity {
private static final String APP_ID = "xxxxxxxxxxxxxxxx";
private static final String[] PERMISSIONS = new String[] {"publish_stream"};

private static final String TOKEN = "access_token";
    private static final String EXPIRES = "expires_in";
    private static final String KEY = "facebook-credentials";

private Facebook facebook;
private String messageToPost;

public boolean saveCredentials(Facebook facebook) {
        Editor editor = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
        editor.putString(TOKEN, facebook.getAccessToken());
        editor.putLong(EXPIRES, facebook.getAccessExpires());
        return editor.commit();
    }

    public boolean restoreCredentials(Facebook facebook) {
        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE);
        facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
        facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
        return facebook.isSessionValid();
    }

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    facebook = new Facebook(APP_ID);
    restoreCredentials(facebook);

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.main);

    String facebookMessage = getIntent().getStringExtra("facebookMessage");
    if (facebookMessage == null){
        facebookMessage = "Test wall post";
    }
    messageToPost = facebookMessage;
}

public void doNotShare(View button){
    finish();
}
public void share(View button){
    if (! facebook.isSessionValid()) {
        loginAndPostToWall();
    }
    else {
        postToWall(messageToPost);
    }
}

public void loginAndPostToWall(){
     facebook.authorize(this, PERMISSIONS, new LoginDialogListener());
}

public void postToWall(String message){
    Bundle parameters = new Bundle();
        parameters.putString("message", message);
        facebook.dialog(this, "stream.publish", parameters, new WallPostDialogListener());
}

class LoginDialogListener implements DialogListener {
    public void onComplete(Bundle values) {
        saveCredentials(facebook);
        if (messageToPost != null){
        postToWall(messageToPost);
    }
    }
    public void onFacebookError(FacebookError error) {
        showToast("Authentication with Facebook failed!");
        //finish();
    }
    public void onError(DialogError error) {
        showToast("Authentication with Facebook failed!");
        //finish();
    }
    public void onCancel() {
        showToast("Authentication with Facebook cancelled!");
        //finish();
    }
}

class WallPostDialogListener implements DialogListener {
    public void onComplete(Bundle values) {
                final String postId = values.getString("post_id");
                if (postId != null) {
                showToast("Message posted to your facebook wall!");
            } else {
                showToast("Wall post cancelled!");
            }
            finish();
        }
    public void onFacebookError(FacebookError e) {
        showToast("Failed to post to wall!");
        e.printStackTrace();
        finish();
    }
    public void onError(DialogError e) {
        showToast("Failed to post to wall!");
        e.printStackTrace();
        finish();
    }
    public void onCancel() {
        showToast("Wall post cancelled!");
        finish();
    }
    }

private void showToast(String message){
    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}
}

不幸的是,每次我提出请求..。窗口只会在几秒钟内闪烁,然后就会结束...

后来,当我在模拟器上删除facebook应用程序时,它工作得很好,我可以在墙上发布新的消息……

这有什么问题吗?我希望你们很快就会知道

把你装满!

EN

回答 1

Stack Overflow用户

发布于 2012-03-03 12:57:16

我为你的问题找到了一个解决方案;请看这里:

代码语言:javascript
复制
public void loginAndPostToWall() {
    facebook.authorize(PostwallActivity.this, PERMISSIONS,
        new LoginDialogListener());
}

以前使用此命令进行登录的教程将此行替换为:

代码语言:javascript
复制
public void loginAndPostToWall() {
    facebook.authorize(PostwallActivity.this, 
        PERMISSIONS,Facebook.FORCE_DIALOG_AUTH,new LoginDialogListener());
}

你得到了一个输出,并且只得到了你的应用程序登录页面,而不是facebook应用程序的主登录页面。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5786938

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档