Facebook SDK(4.x):AccessToken.getCurrentAccessToken()在运行期间返回null,在调试模式下不为null

时间:2015-06-19 12:18:22

标签: android facebook

方法一:在这种情况下,我将一些占位符对象设置为当前的accessToken和Profile

    FacebookSdk.sdkInitialize(getApplicationContext());
    // Initialize the SDK before executing any other operations,
    // especially, if you're using Facebook UI elements.

    AccessToken accessToken = AccessToken.getCurrentAccessToken();
    Profile profile = Profile.getCurrentProfile();

    //Attempts to recognize if the user has logged in before
    if(accessToken !=null){
        if(profile !=null) {
            //Set up user parceable

            //Start the Intent for the next Activity
            //In the final code this intent will be the 3 tabbed activity (Faves, Nearby, Specials)
            //For now its just navigating to nearby
            Intent intent = new Intent(Splash.this, nearbyPlacesHolder.class);
            startActivity(intent);
            finish();
        }
        else{

        }
    }

方法二:我只是使用if语句检查getCurrentAccessToken()== null,如果确实如此,我将boolean isLogged设置为false。如果getCurrentAccessToken()== null条件返回,则将isLoggedIn设置为true,然后在isLoggedIn上执行另一个if语句。

问题是在调试模式内部代码运行完美,但是当我运行它时,没有去,应用程序保持我的登录活动并要求用户执行登录。但是它没有提示进行实际登录,它只是覆盖一个旋转的圆圈,然后继续。很明显,配置文件和访问权限都在那里。

enter image description here

1 个答案:

答案 0 :(得分:0)

在我成功登录到facebook后,我没有尝试再次检索配置文件,而是将所有我想要的信息(包括FB Id)保存到 SQLite DB 。没有问题了。

旧:

截至目前,此代码正在95%的时间内捕获isLoggedIn。不是百分之百。我认为这只是一个计时问题,我希望有一种方法可以确保在所有的facbook资源都准备就绪之前不会调用isLoggedIn()。

@Override
protected void onCreate(Bundle savedInstanceState) {
    //Remove title bar
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    //set the view to the saved data and change the layout
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    FacebookSdk.sdkInitialize(getApplicationContext());
    // Initialize the SDK before executing any other operations,
    // especially, if you're using Facebook UI elements.



    /*~~~~~~    Google API Setup                           */
    mGoogleApiClient = new GoogleApiClient.Builder(Splash.this)
            .addConnectionCallbacks(Splash.this)
            .addOnConnectionFailedListener(Splash.this)
            .addApi(Plus.API)
            .addScope(Plus.SCOPE_PLUS_PROFILE)
            .build();

    /*~~~~~~~~~~~~~~    Facebook SDK setup and login event handler            */






    callbackManager = CallbackManager.Factory.create();








    //needs to be set in on activity result
    LoginManager.getInstance().registerCallback(callbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {

                    Intent intent = new Intent(Splash.this, nearbyPlacesHolder.class);
                    startActivity(intent);
                }

                @Override
                public void onCancel() {
                    // App code
                    Log.d("FBLOGIN", "Cancel");
                    Toast.makeText(Splash.this, "Facebook Login Canceled", Toast.LENGTH_SHORT).show();
                }

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

                }
            });






    //initializes the Facebook SeekBar Event Handlers
    //May be adding noises in here at a later date
    final SeekBar fb_seekbar = (SeekBar)findViewById(R.id.seekbar_fb);
    fb_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            fb_progress = progress;


        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            if (fb_progress <= 75) {
                fb_seekbar.setProgress(4);
            } else {
                fb_seekbar.setProgress(100);
                //This is where the facebook sign in will be performed
                LoginManager.getInstance().logInWithReadPermissions(Splash.this, Arrays.asList("public_profile", "user_friends"));


                //Start the Intent for the next Activity
                //In the final code this intent will be the 3 tabbed activity (Faves, Nearby, Specials)
                //For now its just navigating to nearby
                //Intent intent = new Intent(Splash.this, nearbyPlacesHolder.class);
                //startActivity(intent);
            }
        }
    });

    //initializes the Google Plus SeekBar Event Handlers
    //May be adding noises in here at a later date
    final SeekBar google_slider = (SeekBar)findViewById(R.id.seekbar_google);
    google_slider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            google_progress = progress;

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            if (google_progress <= 75) {
                google_slider.setProgress(0);
            } else {
                google_slider.setProgress(100);

                mGoogleApiClient.connect();

            }
        }
    });

    //initializes the Sign Up SeekBar Event Handlers
    //May be adding noises in here at a later date
    final SeekBar sign_up_seekbar = (SeekBar)findViewById(R.id.seekbar_regular);
    sign_up_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            sign_up_progress = progress;

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            if (sign_up_progress <= 75) {
                sign_up_seekbar.setProgress(4);
            } else {
                sign_up_seekbar.setProgress(100);
                //This is where the facebook sign in will be performed



                //Start the Intent for the next Activity
                //In the final code this intent will be the 3 tabbed activity (Faves, Nearby, Specials)
                //For now its just navigating to nearby
                //Intent intent = new Intent(Splash.this, makeProfile.class);
                //startActivity(intent);
            }
        }
    });


    if(isLoggedIn()){
        //Start the Intent for the next Activity
        //In the final code this intent will be the 3 tabbed activity (Faves, Nearby, Specials)
        //For now its just navigating to nearby
        Intent intent = new Intent(Splash.this, nearbyPlacesHolder.class);
        startActivity(intent);
        finish();
    }



}

public boolean isLoggedIn(){
    AccessToken accessToken = AccessToken.getCurrentAccessToken();
    Profile profile = Profile.getCurrentProfile();
    //Attempts to recognize if the user has logged in before
    if(accessToken !=null && profile !=null) {
            return true;
        }
        else{
            return false;
        }
}