1

我使用Facebook登錄登錄我的Android應用程序。在這裏,我檢查Access Token是否爲空,以便我可以在登錄後移動到另一個活動。這工作正常。請幫助有沒有更好的方式來移動另一個活動,當Facebook已經登錄?如果已登錄,最好的方法是跳過Facebook的登錄頁面

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_boringmain); 

    callbackManager = CallbackManager.Factory.create(); 
    List<String> permissions = new ArrayList<>(); 
    permissions.add("email"); 

    loginButton = (LoginButton) findViewById(R.id.login_button); 
    loginButton.setReadPermissions(permissions); 

    profileTracker = new ProfileTracker() { 
     @Override 
     protected void onCurrentProfileChanged(Profile profile, Profile profile1) { 
      Profile.setCurrentProfile(profile1); 
     } 
    }; 

    profileTracker.startTracking(); 

    accessTokenTracker = new AccessTokenTracker() { 
     @Override 
     protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken,AccessToken currentAccessToken) { 
       AccessToken.setCurrentAccessToken(currentAccessToken); 
     } 
    }; 

    if (AccessToken.getCurrentAccessToken()!=null) { 
     Intent intent = new Intent(boringmain.this, UserProfile.class); 
     startActivity(intent); 
     } 
    else 
     { 
     Log.e("S","User not logged in "); 
     } 

    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
     @Override 
     public void onSuccess(LoginResult loginResult) { 
      getUserDetails(loginResult); 

     }; 

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

有沒有更好的方法做上面的場景?配置文件跟蹤器和訪問令牌跟蹤器的用法是否正確?

回答

0

檢查,如果用戶是通過調用

if (AccessToken.getCurrentAccessToken()!=null) 

是很好的方式登錄。但在我看來更好的地方來檢查它是在打開登錄活動之前。你到處都可以調用這個代碼,你想,如果AccessToken.getCurrentAccessToken()然後打開登錄活動

在登錄活動,你應該登錄成功後,在這裏去別的活動:

loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
    @Override 
    public void onSuccess(LoginResult loginResult) { 
     //START NEW ACTIVITY AFTER CORRECT LOGIN HERE 

     Intent intent = new Intent(boringmain.this, UserProfile.class); 
     startActivity(intent); 
    }; 

    @Override 
    public void onCancel() { 
    }; 
    });