簡體   English   中英

無法使用Facebook SDK登錄

[英]Unable to login using Facebook SDK

在我的應用中,我正在嘗試實現Facebook登錄。 啟動后我有一個IntroLogin活動,以確定用戶是否已經登錄。 如果用戶已經登錄,它將直接加載MainActivity。 否則,它要求用戶登錄。

我確定用戶是否已登錄的方法在Android 6+中運行良好。 但是,在Android Lollipop中,在用戶登錄后(用戶獲得使應用程序進行身份驗證以在彈出窗口中使用那里的信息),該應用程序將關閉。 我在日志中沒有FC或任何消息。 它剛剛關閉。 即使用戶重新啟動應用程序,它也會再次提示登錄。

我的IntroLogin活動:

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

    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

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

    FacebookSdk.sdkInitialize(getApplicationContext());
    callbackManager = CallbackManager.Factory.create();

    AccessToken accessToken = AccessToken.getCurrentAccessToken();
    if(accessToken != null){
        finish();
    }
    else{
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PopupLogin();
            }
        });
    }
}

public void PopupLogin() {
    // Set permissions
    LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile"));
    LoginManager.getInstance().setLoginBehavior(LoginBehavior.WEB_VIEW_ONLY).registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            Log.i(TAG, "onSuccess: " + loginResult);
            startActivity(new Intent(IntroLogin.this, MainActivity.class));
            finish();
        }

        @Override
        public void onCancel() {
            Log.d("MyApp", "Login canceled");
            Snackbar snackbar = Snackbar.make(loginButton, "Login process canceled", Snackbar.LENGTH_INDEFINITE);
            snackbar.getView().setBackgroundColor(ContextCompat.getColor(IntroLogin.this, R.color.colorPrimaryDark));
            snackbar.setActionTextColor(ContextCompat.getColor(IntroLogin.this, R.color.pure_white));
            snackbar.setAction(R.string.retry_login, new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    loginButton.performClick();
                }
            });
            snackbar.show();
        }

        @Override
        public void onError(FacebookException error) {
            Log.d("Myapp", error.toString());
            Toast.makeText(IntroLogin.this, error.getMessage(), Toast.LENGTH_LONG).show();
        }
    });
}

更新:

我找到了原因,但沒有找到解決方案,我認為在棒棒糖中未調用OnActivityResult ,這也許是我沒有看到任何日志的原因。

您應該在調用super.oncreate()之后立即初始化facebooksdk

而且,當accessToken != null時,您正在調用finish() ,這意味着當用戶已經登錄Facebook時,它將關閉活動。

將您的onCreate()方法代碼更改為:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        setContentView(R.layout.activity_intro_login);
        callbackManager = CallbackManager.Factory.create();
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

         loginButton = (Button) findViewById(R.id.login_button);
         updateWithToken(AccessToken.getCurrentAccessToken());
        accessTokenTracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {
                updateWithToken(newToken);
            }
        };
        accessTokenTracker.startTracking();
    }

private void updateWithToken(AccessToken currentAccessToken) {

        if (currentAccessToken != null) {
            Log.d("FB", "User Logged IN.");
            //programetically logout user or do your on success
            LoginManager.getInstance().logOut();
        } else {
            Log.d("FB", "User Logged Out.");
            loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PopupLogin();
            }
        });
      }
  }

找出原因。 我在我的loginActivity中添加了android:launchMode="singleTop" ,這就是OnActivityResult未調用的原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM