数码资讯
Facebook 接入之登录接入
选购提示
关注价格、性能、续航、售后和真实使用场景,理性比较后再下单。
1. 接入Facebook ,首先要先下载Facebook的 SDK,地址(https://developers.facebook.com/docs/android?locale=zh_CN)
下载一个他的示例demo,查看其中的主要的类和方法的使用
下面我新建了一个Android项目实现了 Facebook的 登录功能
a) 首先导入Facebook的sdk ,
b) 新建一个Android工程,引入这个sdk
c) 到AndroidManifest.xml 里面将以下内容复制进去
<!-- beigin、、、、、 FaceBook 的各个配置 -->
<!-- 应用的 app_id 开发者申请的 -->
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/app_id" />
<!-- FaceBook的 登录界面 -->
<activity
android:name="com.facebook.LoginActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<!-- end、、、、、 FaceBook 的各个配置 -->
这其中包含了你申请的 app_id 和 Facebook的Sdk中的登录使用的Activity。
将申请到的appId放到string.xml 里
<string name="app_id">355198514515820</string>
新建一个Activity,也可以在MainActivity里面添加一个布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:andro
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.facebook.widget.LoginButton
android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
这里面包含一个Facebook SDK里面已经定义好的 登录按钮
回到Activity界面,添加以下代码
private LoginButton loginButton = null;
private UiLifecycleHelper uiHelper = null;
@Override
protected void onResume() {
super.onResume();
uiHelper.onResume();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
g) 在OnCreate方法里面初始化一下Facebook的几个方法
loginButton = (LoginButton) this.findViewById(R.id.loginButton);
// 设置权限
loginButton.setReadPermissions(permissions);
/** 初始化 */
private void initFacebook(Bundle savedInstanceState) {
uiHelper = new UiLifecycleHelper(this, callback);
uiHelper.onCreate(savedInstanceState);
}
StatusCallback callback = new Session.StatusCallback() {
// Session 必须在 Request 之前 打开,
@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()) {
currentSession = session;
fetchUserInfo();
}
}
/** 抓取 用户信息 */
private void fetchUserInfo() {
if (currentSession != null && currentSession.isOpened()) {
Request request = Request.newMeRequest(currentSession,
new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user,
Response response) {
if (response.getRequest().getSession() == currentSession) {
Log.e("First Name", user.getFirstName());
Log.e("Last Name", user.getLastName());
Log.e("id ", user.getId());
Log.e("User Name", "" + user.getUsername());
Log.e("Inner JSONObject", "" + user.getInnerJSONObject().toString());
Log.e("Birthday", "" + user.getBirthday());
Log.e("Link", "" + user.getLink());
Log.e("Name", "" + user.getName());
Log.e("property", "" + user.getProperty("name"));
}
}
});
request.executeAsync();
}
}
如果感觉有些乱的话,可以参看工程:http://download.csdn.net/detail/yingshetou123/8416481
声明:本文内容用于数码产品信息整理与选购参考,具体价格、库存、售后政策以官方渠道和电商页面实时信息为准。