2016-12-06 11 views
0

アンドロイドアプリでFacebookログインを実装しようとしています。私はFacebookデベロッパーガイドに従っていますが、Facebookアプリがインストールされていないと、ログインの詳細が表示され、ログインして同じアクティビティにとどまり、ログアウトボタンが表示されます。アプリがインストールされていない場合、Facbookのログインは2番目のアクティビティを開始しません

public class MainActivity extends AppCompatActivity { 

LoginButton loginButton; 
CallbackManager callbackManager; 



@Override 
protected void onCreate(Bundle savedInstanceState) { 

    FacebookSdk.sdkInitialize(getApplicationContext()); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    isLoggedIn(); 

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

    callbackManager = CallbackManager.Factory.create(); 

    LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
     @Override 
     public void onSuccess(LoginResult loginResult) { 
      AccessToken accessToken = loginResult.getAccessToken(); 
      Profile profile = Profile.getCurrentProfile(); 
      nextActivity(profile); 

     } 

     @Override 
     public void onCancel() { 

     } 

     @Override 
     public void onError(FacebookException error) { 

     } 
    }); 

    loginButton.setReadPermissions("user_friends"); 



} 

protected void onActivityResult(int requestCode, int responseCode, Intent intent) { 
    super.onActivityResult(requestCode, responseCode, intent); 
    //Facebook login 
    callbackManager.onActivityResult(requestCode, responseCode, intent); 

} 

public void isLoggedIn() { 

    AccessToken accessToken = AccessToken.getCurrentAccessToken(); 
    if (accessToken!=null) { 

     Profile profile=Profile.getCurrentProfile(); 

     nextActivity(profile); 
    } 
} 

private void nextActivity(Profile profile) { 
    if (profile != null) {                      Intent main = new Intent(MainActivity.this, Result.class); 
     main.putExtra("Name", profile.getFirstName()); 
     main.putExtra("Surname", profile.getLastName()); 
     startActivity(main); 
     finish(); 
    } 
} 

}

答えて

0

の代わりにEDIT LoginManager.getInstance().registerCallback()使用loginButton.registerCallback()

を使用して:

インサイドするonSuccessコードを次ています

@Override 
    public void onSuccess(LoginResult loginResult) { 
     AccessToken accessToken = loginResult.getAccessToken(); 
     if(Profile.getCurrentProfile() == null) { 
       ProfileTracker profileTracker = new ProfileTracker() { 
         @Override 
         protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) { 
           this.stopTracking(); 
           nextActivity(currentProfile); 
         } 
       }; 
       profileTracker.startTracking(); 
     }else{ 
       Profile profile = Profile.getCurrentProfile(); 
       nextActivity(profile); 
     } 
+0

はまだ – MateenSheikh

+0

がするonSuccessグラムされているのと同じ問題を抱えて呼ばれるかどうか?あなたがaccesstokenとprofileのためにどんな価値を得ているのか?また、あなたが意図した "main"を初期化する場所は? – Rahil2952

+0

はいonSuccessが呼び出されていて、プロファイルが** null **値を示していて、accesstokenが表示されています** {AccessTokenトークン:ACCESS_TOKEN_REMOVED権限:[user_friends、public_profile]} ** インテントはnextActivityメソッドで初期化されました。 。 – MateenSheikh

関連する問題