Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check users already logged-in in android (Facebook SDK 4.0)

Here is my code:

public class LocationDetailActivity extends ActionBarActivity {

private CallbackManager mCallBackManager;
private FacebookCallback<LoginResult> mCallBack;
private ImageView mBtnBack;
AccessToken token;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.location_detail_layout);
    FacebookSdk.sdkInitialize(getApplicationContext());
    share = (ImageButton) findViewById(R.id.imageShare);
    token  = AccessToken.getCurrentAccessToken();

 share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(token == null) {
                    final Dialog dialog = new Dialog(LocationDetailActivity.this);
                    dialog.setContentView(R.layout.share_custom_dialog);
                    dialog.setTitle("Login as:");
                    dialog.show();
                    mCallBackManager = CallbackManager.Factory.create();
                    mCallBack = new FacebookCallback<LoginResult>() {
                        @Override
                        public void onSuccess(LoginResult loginResult) {

                            Profile profile = Profile.getCurrentProfile();
                            if(profile!=null){
                                dialog.cancel();
                                ShareDialog shareDialog = new ShareDialog(LocationDetailActivity.this);
                                if(shareDialog.canShow(ShareLinkContent.class)){
                                ShareLinkContent content = new ShareLinkContent.Builder()
                                        .setContentUrl(Uri.parse("https://developers.facebook.com"))
                                        .build();
                                    shareDialog.show(content);
                                }
                            }
                        }

                        @Override
                        public void onCancel() {

                        }

                        @Override
                        public void onError(FacebookException e) {

                        }
                    };
                    LoginButton loginButton = (LoginButton)dialog.findViewById(R.id.login_button);
                    loginButton.setReadPermissions("user_friends");
                    loginButton.registerCallback(mCallBackManager, mCallBack);
                }
                else if(token != null){
                    ShareDialog shareDialog = new ShareDialog(LocationDetailActivity.this);
                    if(shareDialog.canShow(ShareLinkContent.class)){
                        ShareLinkContent content = new ShareLinkContent.Builder()
                                .setContentUrl(Uri.parse("https://developers.facebook.com"))
                                .build();
                        shareDialog.show(content);
                    }
                }
            }
        });

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    mCallBackManager.onActivityResult(requestCode, resultCode, data);
}
}

I want to check if user already logged in from Facebook's app. If they didn't, my app will show a Custom Dialog with login button. Otherwise, my app will show Share Dialog, but when I click on my button, the Custom Dialog always be showed even I logged-in Facebook.

More info: AccessToken token = AccessToken.getCurrentAccessToken();

like image 332
naji kun Avatar asked Nov 10 '22 14:11

naji kun


1 Answers

Check Login Status

boolean isLoggedIn = accessToken != null && !accessToken.isExpired();

Then you can later perform the actual login, such as in a custom button's OnClickListener:

LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile"));

like image 196
Vikram vora Avatar answered Nov 14 '22 22:11

Vikram vora