How to check if user is logged in with FB SDK 4.0 for Android?

前端 未结 6 1764
走了就别回头了
走了就别回头了 2020-11-30 18:56

A few days ago I implemented FB Login to my APP, and today I found out that most of the things I have implemented are now deprecated.

Before, I was using Sess

6条回答
  •  醉酒成梦
    2020-11-30 19:11

    Its late to reply, but now in version 4.25.0 of Facebook SDK there is a method:

    public void retrieveLoginStatus(Context context,
                                    LoginStatusCallback responseCallback)
    

    Which states:

    Retrieves the login status for the user. This will return an access token for the app if a user is logged into the Facebook for Android app on the same device and that user had previously logged into the app. If an access token was retrieved then a toast will be shown telling the user that they have been logged in.

    And can be used like:

    LoginManager.getInstance().retrieveLoginStatus( this, new LoginStatusCallback()
    {
        @Override
        public void onCompleted( AccessToken accessToken )
        {
            GraphRequest request = GraphRequest.newMeRequest( accessToken, new GraphRequest.GraphJSONObjectCallback()
            {
                @Override
                public void onCompleted( JSONObject object, GraphResponse response )
                {
                    Log.e( TAG, object.toString() );
                    Log.e( TAG, response.toString() );
    
                    try
                    {
                        userId = object.getString( "id" );
                        profilePicture = new URL( "https://graph.facebook.com/" + userId + "/picture?width=500&height=500" );
                        Log.d( "PROFILE_URL", "url: " + profilePicture.toString() );
                        if ( object.has( "first_name" ) )
                        {
                            firstName = object.getString( "first_name" );
                        }
                        if ( object.has( "last_name" ) )
                        {
                            lastName = object.getString( "last_name" );
                        }
                        if ( object.has( "email" ) )
                        {
                            email = object.getString( "email" );
                        }
                        if ( object.has( "birthday" ) )
                        {
                            birthday = object.getString( "birthday" );
                        }
                        if ( object.has( "gender" ) )
                        {
                            gender = object.getString( "gender" );
                        }
    
                        Intent main = new Intent( LoginActivity.this, MainActivity.class );
                        main.putExtra( "name", firstName );
                        main.putExtra( "surname", lastName );
                        main.putExtra( "imageUrl", profilePicture.toString() );
                        startActivity( main );
                        finish();
                    }
                    catch ( JSONException e )
                    {
                        e.printStackTrace();
                    }
                    catch ( MalformedURLException e )
                    {
                        e.printStackTrace();
                    }
    
                }
            } );
            //Here we put the requested fields to be returned from the JSONObject
            Bundle parameters = new Bundle();
            parameters.putString( "fields", "id, first_name, last_name, email, birthday, gender" );
            request.setParameters( parameters );
            request.executeAsync();
        }
    
        @Override
        public void onFailure()
        {
            Toast.makeText( LoginActivity.this, "Could not log in.", Toast.LENGTH_SHORT ).show();
        }
    
        @Override
        public void onError( Exception exception )
        {
            Toast.makeText( LoginActivity.this, "Could not log in.", Toast.LENGTH_SHORT ).show();
        }
    } );
    

提交回复
热议问题