Android Facebook Profile.getCurrentProfile()在首次登录后始终返回null

Android Facebook Profile.getCurrentProfile()在首次登录后始终返回null,android,facebook-graph-api,facebook-android-sdk,android-facebook,Android,Facebook Graph Api,Facebook Android Sdk,Android Facebook,当我第一次通过Facebook登录应用程序时,我从profile.getCurrentProfile()获取个人资料 当我退出应用程序并再次启动时,它已经登录。所以我可以直接调用Profile.getCurrentProfile()返回null 代码 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FacebookSdk.sdkIn

当我第一次通过Facebook登录应用程序时,我从
profile.getCurrentProfile()获取个人资料

当我退出应用程序并再次启动时,它已经登录。所以我可以直接调用
Profile.getCurrentProfile()返回null

代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());
    setContentView(R.layout.activity_page);
    Profile profile = Profile.getCurrentProfile();
    // For the first launch the profile will be null
    displayProfileName(profile);
    LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
    loginButton.setReadPermissions("public_profile");
    callbackManager = CallbackManager.Factory.create();
    loginButton.registerCallback(callbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(final LoginResult loginResult) {
                    profileTracker = new ProfileTracker() {

                        @Override
                        protected void onCurrentProfileChanged(
                                Profile oldProfile, Profile currentProfile) {
                            profileTracker.stopTracking();
                            Profile.setCurrentProfile(currentProfile);
                            Profile profile = Profile.getCurrentProfile();
                            displayProfileName(profile);
                        }
                    };
                    profileTracker.startTracking();
                }

                @Override
                public void onCancel() {
                }

                @Override
                public void onError(FacebookException exception) {
                }
            });
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (profileTracker != null) {
        profileTracker.stopTracking();
    }
}

@Override
protected void onPause() {
    super.onPause();
    // Logs 'app deactivate' App Event.
    AppEventsLogger.deactivateApp(this);
}

@Override
protected void onRestart() {
    super.onRestart();
    AppEventsLogger.activateApp(this);
}
/**
*
* Method to display the Profile Name 
*/
private void displayProfileName(Profile profile) {
    if (profile != null) {
        Toast.makeText(MainActivity.this, profile.getName(),
                Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(MainActivity.this, "No Profile", Toast.LENGTH_LONG)
                .show();
    }
}


@Override
protected void onActivityResult(int arg0, int arg1, Intent arg2) {
    super.onActivityResult(arg0, arg1, arg2);
    callbackManager.onActivityResult(arg0, arg1, arg2);
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
sdkinInitialize(getApplicationContext());
setContentView(R.layout.activity_页面);
Profile Profile=Profile.getCurrentProfile();
//对于第一次启动,配置文件将为空
显示配置文件名(profile);
LoginButton LoginButton=(LoginButton)findViewById(R.id.login_按钮);
setReadPermissions(“public_profile”);
callbackManager=callbackManager.Factory.create();
loginButton.registerCallback(callbackManager,
新建FacebookCallback(){
@凌驾
成功时公开作废(最终登录结果登录结果){
profileTracker=新的profileTracker(){
@凌驾
更改CurrentProfile时受保护的无效(
配置文件oldProfile、配置文件currentProfile){
profileTracker.stopTracking();
Profile.setCurrentProfile(currentProfile);
Profile Profile=Profile.getCurrentProfile();
显示配置文件名(profile);
}
};
profileTracker.startTracking();
}
@凌驾
公开作废{
}
@凌驾
public void onError(facebook异常){
}
});
}
@凌驾
受保护的空onDestroy(){
super.ondestory();
if(profileTracker!=null){
profileTracker.stopTracking();
}
}
@凌驾
受保护的void onPause(){
super.onPause();
//记录“应用程序停用”应用程序事件。
AppEventsLogger.停用EAPP(本);
}
@凌驾
受保护的void onRestart(){
super.onRestart();
AppEventsLogger.activateApp(此);
}
/**
*
*方法来显示配置文件名称
*/
私有void displayProfileName(配置文件){
if(profile!=null){
Toast.makeText(MainActivity.this,profile.getName(),
Toast.LENGTH_LONG).show();
}否则{
Toast.makeText(MainActivity.this,“无配置文件”,Toast.LENGTH\u LONG)
.show();
}
}
@凌驾
ActivityResult上的受保护无效(int arg0、int arg1、Intent arg2){
super.onActivityResult(arg0、arg1、arg2);
callbackManager.onActivityResult(arg0、arg1、arg2);
}

调用
LoginManager.getInstance().logOut()一旦获得配置文件详细信息。

这种情况有两种:

  • 对于第一次登录,请遵循
  • 对于第二次登录,您需要刷新
    AccessToken
    ,然后获取配置文件。刷新令牌代码可以在中找到,但下面的代码中有它(为了简化)
  • 该代码取自)

    你可以把这段代码直接放到你的应用程序中,上面的注释写着“案例1”,只要调用你的普通FB登录即可

    private AccessTokenTracker mAccessTokenTracker;
    
    private void loginToMyFbApp() {
        FacebookSdk.sdkInitialize(this);
        if (AccessToken.getCurrentAccessToken() != null) {
            mAccessTokenTracker = new AccessTokenTracker() {
                @Override
                protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
                    mAccessTokenTracker.stopTracking();
                    if(currentAccessToken == null) {
                        //(the user has revoked your permissions -
                        //by going to his settings and deleted your app)
                        //do the simple login to FaceBook
                        //case 1
                    }
                    else {
                        //you've got the new access token now.
                        //AccessToken.getToken() could be same for both
                        //parameters but you should only use "currentAccessToken"
                        //case 2
                        fetchProfile();
                    }
                }
            };
            mAccessTokenTracker.startTracking();
            AccessToken.refreshCurrentAccessTokenAsync();
        }
        else {
            //do the simple login to FaceBook
            //case 1
        }
    }
    
    private void fetchProfile() {
        GraphRequest request = GraphRequest.newMeRequest(
                AccessToken.getCurrentAccessToken(),
                new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(JSONObject object, GraphResponse response) {
                        // this is where you should have the profile
                        Log.v("fetched info", object.toString());
                    }
                });
        Bundle parameters = new Bundle();
        parameters.putString("fields", "id,name,link"); //write the fields you need
        request.setParameters(parameters);
        request.executeAsync();
    }
    

    这方面有两种情况:

  • 对于第一次登录,请遵循
  • 对于第二次登录,您需要刷新
    AccessToken
    ,然后获取配置文件。刷新令牌代码可以在中找到,但下面的代码中有它(为了简化)
  • 该代码取自)

    你可以把这段代码直接放到你的应用程序中,上面的注释写着“案例1”,只要调用你的普通FB登录即可

    private AccessTokenTracker mAccessTokenTracker;
    
    private void loginToMyFbApp() {
        FacebookSdk.sdkInitialize(this);
        if (AccessToken.getCurrentAccessToken() != null) {
            mAccessTokenTracker = new AccessTokenTracker() {
                @Override
                protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
                    mAccessTokenTracker.stopTracking();
                    if(currentAccessToken == null) {
                        //(the user has revoked your permissions -
                        //by going to his settings and deleted your app)
                        //do the simple login to FaceBook
                        //case 1
                    }
                    else {
                        //you've got the new access token now.
                        //AccessToken.getToken() could be same for both
                        //parameters but you should only use "currentAccessToken"
                        //case 2
                        fetchProfile();
                    }
                }
            };
            mAccessTokenTracker.startTracking();
            AccessToken.refreshCurrentAccessTokenAsync();
        }
        else {
            //do the simple login to FaceBook
            //case 1
        }
    }
    
    private void fetchProfile() {
        GraphRequest request = GraphRequest.newMeRequest(
                AccessToken.getCurrentAccessToken(),
                new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(JSONObject object, GraphResponse response) {
                        // this is where you should have the profile
                        Log.v("fetched info", object.toString());
                    }
                });
        Bundle parameters = new Bundle();
        parameters.putString("fields", "id,name,link"); //write the fields you need
        request.setParameters(parameters);
        request.executeAsync();
    }
    

    而不是访问Profile.getCurrentProfile()

    在onSuccess()中使用访问令牌并发出图形请求

    (它对我有用)

    下面是一段代码:

    FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
    
            Log.v("profile track", (DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT).format(loginResult.getAccessToken().getExpires())));
            GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response) {
                            try {
    
                                String name = object.getString("name");
                                String email = object.getString("email");
                                String id = object.getString("id");
                                Toast.makeText(Login.this, name + " " + " " + email + " " + id, Toast.LENGTH_SHORT).show();
    
                        /*write  your code  that is to be executed after successful login*/
    
    
                            } catch (JSONException ex) {
                                ex.printStackTrace();
                            }
                        }
                    });
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name,email,gender, birthday");
            request.setParameters(parameters);
            request.executeAsync();
        }
    
        @Override
        public void onCancel() {
        }
    
        @Override
        public void onError(FacebookException e) {
        }
    };
    
    FacebookCallback callback=new FacebookCallback(){
    @凌驾
    成功时公共无效(LoginResult LoginResult){
    Log.v(“profile track”,(DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.SHORT).format(loginResult.getAccessToken().getExpires()));
    GraphRequest请求=GraphRequest.NewMereRequest(loginResult.getAccessToken(),
    新建GraphRequest.GraphJSONObjectCallback(){
    @凌驾
    未完成公共无效(JSONObject对象,GraphResponse响应){
    试一试{
    字符串名称=object.getString(“名称”);
    String email=object.getString(“email”);
    stringid=object.getString(“id”);
    Toast.makeText(Login.this,name++++email++id,Toast.LENGTH\u SHORT).show();
    /*编写成功登录后要执行的代码*/
    }捕获(JSONException ex){
    例如printStackTrace();
    }
    }
    });
    Bundle参数=新Bundle();
    参数.putString(“字段”、“id、姓名、电子邮件、性别、生日”);
    请求。设置参数(参数);
    request.executeAsync();
    }
    @凌驾
    公开作废{
    }
    @凌驾
    公共无效onError(FaceBook例外e){
    }
    };
    
    而不是访问Profile.getCurrentProfile()

    在onSuccess()中使用访问令牌并发出图形请求

    (它对我有用)

    下面是一段代码:

    FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
    
            Log.v("profile track", (DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT).format(loginResult.getAccessToken().getExpires())));
            GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response) {
                            try {
    
                                String name = object.getString("name");
                                String email = object.getString("email");
                                String id = object.getString("id");
                                Toast.makeText(Login.this, name + " " + " " + email + " " + id, Toast.LENGTH_SHORT).show();
    
                        /*write  your code  that is to be executed after successful login*/
    
    
                            } catch (JSONException ex) {
                                ex.printStackTrace();
                            }
                        }
                    });
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name,email,gender, birthday");
            request.setParameters(parameters);
            request.executeAsync();
        }
    
        @Override
        public void onCancel() {
        }
    
        @Override
        public void onError(FacebookException e) {
        }
    };
    
    FacebookCallback callback=new FacebookCallback(){
    @凌驾
    成功时公共无效(LoginResult LoginResult){
    Log.v(“profile track”,(DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.SHORT)。format(loginResult.getAc