检查用户是否已登录Facebook

时间:2015-05-30 18:58:26

标签: android facebook facebook-android-sdk facebook-sdk-4.0

如果用户已经使用Facebook登录,是否有方法可以跳过登录类,并直接转到MainActivity?我似乎无法在Facebook SDK文档中找到任何解决方案。我在下面添加了Login类和SplashScreen类。

现在我只能选择退出,然后再次启动应用后再次登录登录类。

登录课程遵循Facebook的指南,我删除了所有代码,使其与StackO的读者相关。

登录类

public class Login extends Activity {

/**
 * Called when the activity is first created.
 */

private CallbackManager callbackManager;
private ProfileTracker profileTracker;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());
    setContentView(R.layout.activity_login);


    callbackManager = CallbackManager.Factory.create();
    LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
    loginButton.setReadPermissions("public_profile", "email", "user_friends");

 profileTracker=new ProfileTracker() {
        @Override
         protected void onCurrentProfileChanged (
                Profile oldProfile,
                Profile currentProfile){
            Log.i("profileTracker", "profileTracker");
        }
    };
    if (profileTracker == null){
        Intent i = new Intent(Login.this, MainActivity.class);
        startActivity(i);
    };


loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {


        @Override
        public void onSuccess(LoginResult loginResult) {

            Intent i = new Intent(Login.this, MainActivity.class);
            startActivity(i);
            System.out.print("Logged in");

        }

        @Override
        public void onCancel() {
            // App code

        }

        @Override
        public void onError(FacebookException exception) {
            // App code
            Log.i("Error" , "Error");
        }


    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    callbackManager.onActivityResult(requestCode, resultCode, data);
    }
}

SplashScreen类

public class SplashScreen extends Activity {

private static final int SPLASH_TIME_OUT = 2000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Fabric.with(this, new Crashlytics());
    setContentView(R.layout.activity_splash_screen);

    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            Intent i = new Intent(SplashScreen.this, Login.class);
            startActivity(i);

            finish();
        }
    }, SPLASH_TIME_OUT);
    }

}

1 个答案:

答案 0 :(得分:1)

-Hello您可以使用Profile Tracker并使用Onstart方法启动它并检查是否存在Profile

- 这是样本 CODE: -

- profileTracker = new ProfileTracker() { @Override protected void onCurrentProfileChanged( Profile oldProfile, Profile currentProfile) { Log.i("accesstokensuccess", "gjk"); } }; profile = Profile.getCurrentProfile(); if( (profile != null) ) { ..you are already logged in..code for your main activity....}

相关问题