如何使用android-simple-facebook登录?

时间:2014-08-21 20:12:46

标签: android facebook android-simple-facebook

我需要获取基本用户数据,如姓名,电子邮件地址,个人资料图片,出生日期等。 我想通过在我的Android应用程序中通过Facebook向用户提供登录而不是要求他们手动输入来从Facebook获取此数据。

我想为此目的使用android-simple-facebook https://github.com/sromku/android-simple-facebook

请有人给我一步一步的指示,告诉我如何做。他们的github帐户的自述文件太模糊,无法清楚地理解每一步。

1 个答案:

答案 0 :(得分:1)

我在Simple Facebook中也遇到了同样的问题。所以我刚刚创建了一个Helper类,并使用它的方法setLogin来登录。

    public class FacebookHelper {

public OnLoginListener onLoginListener = null;
Button btnFacebook;
ProgressDialog mProgressDialog;
Context mContext;

public FacebookHelper(Context mContext) {
    this.mContext = mContext;
}

public void setLogin() {
    // Login listener
    onLoginListener = new OnLoginListener() {

        @Override
        public void onThinking() {
            // TODO Auto-generated method stub
        }

        @Override
        public void onException(Throwable throwable) {
            // TODO Auto-generated method stub
            Toast.makeText(mContext, throwable + "", Toast.LENGTH_SHORT)
                    .show();
        }

        @Override
        public void onFail(String reason) {
            Toast.makeText(mContext, reason + "", Toast.LENGTH_SHORT)
                    .show();

            // TODO Auto-generated method stub
        }

        @Override
        public void onLogin() {

            // TODO Auto-generated method stub
            publishPhoto();
        }

        @Override
        public void onNotAcceptingPermissions(Type type) {
            // TODO Auto-generated method stub
            Toast.makeText(mContext, type + "", Toast.LENGTH_SHORT).show();

        }
    };

}

public void publishPhoto() {

    // set privacy
    Privacy privacy = new Privacy.Builder().setPrivacySettings(
            PrivacySettings.ALL_FRIENDS).build();

    Feed photo = new Feed.Builder()
            .setName("Name")
            .setDescription("Description")
            .setPicture(
                    "http://www.bcre.com/images/laguna_beach_california_2592.jpg")

            .setPrivacy(privacy).build();

    SimpleFacebook.getInstance().publish(photo, true,
            new OnPublishListener() {

                @Override
                public void onException(Throwable throwable) {
                    mProgressDialog.dismiss();
                    Toast.makeText(mContext, throwable.getMessage(),
                            Toast.LENGTH_LONG).show();
                }

                @Override
                public void onFail(String reason) {
                    // mProgressDialog.dismiss();
                    Toast.makeText(mContext, reason, Toast.LENGTH_LONG)
                            .show();
                }

                @Override
                public void onThinking() {
                    mProgressDialog = new ProgressDialog(mContext);
                    mProgressDialog.setTitle("Please Wait");
                    mProgressDialog.setMessage("UpLoading.....");
                    mProgressDialog.setCancelable(false);
                    mProgressDialog.show();
                }

                @Override
                public void onComplete(String response) {
                    // mProgressDialog.dismiss();
                    // Toast.makeText(mOCParksContext, response,
                    // Toast.LENGTH_LONG).show();
                }
            });

}

}

然后调用它的方法只需在你的点击上执行此操作。并在一些覆盖方法中启动对象

mFacebookHelper.setLogin();
    mSimpleFacebook.login(mFacebookHelper.onLoginListener);

    @Override
protected void onResume() {
    super.onResume();
    mSimpleFacebook = SimpleFacebook.getInstance(this);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    mSimpleFacebook.onActivityResult(this, requestCode, resultCode, data);
    super.onActivityResult(requestCode, resultCode, data);

}