내 Android 애플리케이션에서 Facebook 로그인을 사용하고 있습니다. 성공적으로 로그인하고 TextView에 사용자 이름을 표시 할 수 있지만 아래 문제가 있습니다.
응용 프로그램을 닫고 다시 열 때 페이스 북 로그인에 성공하면 로그 아웃 버튼이 표시되지만 프로필 이름이 없습니다. 활동이 사용자 이름을 가져 오기 위해 다시 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에 결과가 표시됩니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다