自Facebook的登录按钮 - 机器人 [英] Custom Facebook Login Button - Android
问题描述
I''m下面这个教程一>但到目前为止,我不能让它工作,虽然这是从一年前左右...
我使用androidStudo 1.2.2和FacebookSDK 4。
我想要一个简单的登录到Facebook的使用自定义的按钮,就像这个图片所示:
现在,从指南中,我有问题的会话
变量的例子,它说,它解决不了的,也不 getActivity( )
有naybody尝试这样的FacebookSDK4.0?
那是一个正确的做法或许有更多的东西更新?
在此先感谢!
第1步:
首先添加的FrameLayout,并让Facebook按钮的知名度=水涨船高,并添加自定义按钮。
不要忘了的xmlns:Facebook的=http://schemas.android.com/apk/res-auto
在主布局
<的FrameLayout
机器人:ID =@ + ID / FrameLayout1
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT>
< com.facebook.login.widget.LoginButton
机器人:ID =@ + ID / login_button
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:能见度=水涨船高/>
<按钮
机器人:ID =@ + ID / FB
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:后台=#416BC1
机器人:的onClick =的onClick
机器人:文本=脸谱
机器人:文字颜色=#FFFFFF
机器人:TEXTSTYLE =黑体/>
< /的FrameLayout>
第二步:
在的onCreate
初始化 FacebookSdk
活用布局之前。
FacebookSdk.sdkInitialize(this.getApplicationContext());
第三步:添加到您的Java文件这个
callbackManager = CallbackManager.Factory.create();
FB =(按钮)findViewById(R.id.fb);
loginButton =(LoginButton)findViewById(R.id.login_button);
名单<字符串> permissionNeeds = Arrays.asList(user_photos,电子邮件,
user_birthday,public_profile,AccessToken);
loginButton.registerCallback(callbackManager,
新FacebookCallback< LoginResult> (){@覆盖
公共无效的onSuccess(LoginResult loginResult){
的System.out.println(的onSuccess);
字符串accessToken = loginResult.getAccessToken()
.getToken();
Log.i(accessToken,accessToken);
GraphRequest请求= GraphRequest.newMeRequest(
loginResult.getAccessToken(),
新GraphRequest.GraphJSONObjectCallback(){@覆盖
公共无效onCompleted(的JSONObject对象,
GraphResponse响应){
Log.i(LoginActivity
response.toString());
尝试 {
ID = object.getString(ID);
尝试 {
URL profile_pic =新的URL(
http://graph.facebook.com/+编号+?/图像类型=大);
Log.i(profile_pic
profile_pic +);
}赶上(MalformedURLException异常E){
e.printStackTrace();
}
名称= object.getString(姓名);
电子邮件= object.getString(电子邮件);
性别= object.getString(性别);
生日= object.getString(生日);
}赶上(JSONException E){
e.printStackTrace();
}
}
});
捆绑参数=新包();
parameters.putString(域,
编号,姓名,电子邮箱,性别,生日);
request.setParameters(参数);
request.executeAsync();
}
@覆盖
公共无效OnCancel的(){
的System.out.println(OnCancel的」);
}
@覆盖
公共无效onerror的(FacebookException除外){
的System.out.println(onError的);
Log.v(LoginActivity,exception.getCause()的toString());
}
});
@覆盖
保护无效onActivityResult(INT申请code,INT响应code,
意图数据){
super.onActivityResult(要求code,响应code,数据);
callbackManager.onActivityResult(要求code,响应code,数据);
}
第五步: 设置您的自定义按钮,点击按钮FacebookLogin点击。
公共无效的onClick(视图v){
如果(V == FB){
loginButton.performClick();
}
}
第6步: 对于编程注销使用。
LoginManager.getInstance()退出();
配置= Profile.getCurrentProfile()getCurrentProfile()。
如果(资料!= NULL){
//用户登录
} 其他 {
//用户没有登录
}
I''m following this Tutorial but so far I can't make it work, though this is from a year ago or so...
I'm using androidStudo 1.2.2 and FacebookSDK 4.
I want a simple login into facebook using a custom button, like the one shown in this image:
Now, in the example from the tutorial I'm having problems with the Session
variable, it says it cannot resolve it, neither getActivity()
Has naybody tried this on FacebookSDK4.0?
Is that a correct approach or maybe there is something more updated?
Thanks in advance!
Step 1:
First add FrameLayout and make facebook button visibility="gone" and add your custom button.
Don't forgot to put xmlns:facebook="http://schemas.android.com/apk/res-auto"
in your main layout.
<FrameLayout
android:id="@+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.facebook.login.widget.LoginButton
android:id="@+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
<Button
android:id="@+id/fb"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#416BC1"
android:onClick="onClick"
android:text="FaceBook"
android:textColor="#ffffff"
android:textStyle="bold" />
</FrameLayout>
Step 2:
Initialize FacebookSdk
in onCreate
before inflecting layout.
FacebookSdk.sdkInitialize(this.getApplicationContext());
Step 3: add this into your java file.
callbackManager = CallbackManager.Factory.create();
fb = (Button) findViewById(R.id.fb);
loginButton = (LoginButton) findViewById(R.id.login_button);
List < String > permissionNeeds = Arrays.asList("user_photos", "email",
"user_birthday", "public_profile", "AccessToken");
loginButton.registerCallback(callbackManager,
new FacebookCallback < LoginResult > () {@Override
public void onSuccess(LoginResult loginResult) {
System.out.println("onSuccess");
String accessToken = loginResult.getAccessToken()
.getToken();
Log.i("accessToken", accessToken);
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {@Override
public void onCompleted(JSONObject object,
GraphResponse response) {
Log.i("LoginActivity",
response.toString());
try {
id = object.getString("id");
try {
URL profile_pic = new URL(
"http://graph.facebook.com/" + id + "/picture?type=large");
Log.i("profile_pic",
profile_pic + "");
} catch (MalformedURLException e) {
e.printStackTrace();
}
name = object.getString("name");
email = object.getString("email");
gender = object.getString("gender");
birthday = object.getString("birthday");
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields",
"id,name,email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
System.out.println("onCancel");
}
@Override
public void onError(FacebookException exception) {
System.out.println("onError");
Log.v("LoginActivity", exception.getCause().toString());
}
});
Step 4: Don't forget to add following code.
@Override
protected void onActivityResult(int requestCode, int responseCode,
Intent data) {
super.onActivityResult(requestCode, responseCode, data);
callbackManager.onActivityResult(requestCode, responseCode, data);
}
Step 5: Set your custom button click to FacebookLogin button click.
public void onClick(View v) {
if (v == fb) {
loginButton.performClick();
}
}
Step 6: For programmatically logout use this.
LoginManager.getInstance().logOut();
Step 7: you can find user logged in or not by profile.
profile = Profile.getCurrentProfile().getCurrentProfile();
if (profile != null) {
// user has logged in
} else {
// user has not logged in
}
这篇关于自Facebook的登录按钮 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!