[英]Get The Facebook Friends List in Android Activity Other then where User Logged In
我在Android應用程序中使用Facebook登錄。 我能夠成功登錄並在TextView中顯示用戶名,但是我面臨以下問題
在成功登錄Facebook之后,當我關閉該應用程序並再次將其打開時,我將獲得注銷按鈕,但配置文件名稱丟失。 我希望活動再次調用facebook API以獲取用戶名。
我在OnCreate事件中使用以下代碼
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
setContentView(R.layout.activity_login);
callbackManager = CallbackManager.Factory.create();
LoginButton loginButton = (LoginButton) findViewById(R.id.connectWithFbButton);
loginButton.setReadPermissions("email", "user_friends");
loginButton.registerCallback(callbackManager, nCallback);
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode,resultCode,data);
}
private FacebookCallback<LoginResult> nCallback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
if ( profile!= null)
{
String FirstName = "";
String LastName = "";
String ProfilePic = "";
String PId = "";
FirstName = URLEncoder.encode(profile.getFirstName(), "utf-8");
LastName = URLEncoder.encode(profile.getLastName(), "utf-8");
message.setText( "Hello " + profile.getFirstName());
}
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException error) {
}
};
我相信你正在尋找這樣的東西
Profile profile;
String name;
try{
profile = Profile.getCurrentProfile();
name = profile.getName();
}catch(NullPointerException npe){
npe.printStackTrace();
}
onSuccess
僅在您登錄時被調用,因此您需要在getCorrentProfile
之外 onSucess
請注意,如果您未登錄,它將返回null
因此在onCreate中檢查getCorrentProfile
是否為null,如果是這樣,讓用戶登錄,您將在onSuccess中獲得結果,否則,您將使用配置文件獲取名稱和其他信息
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.