How to check users who are already logged into 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 the user has already been registered in the Facebook application. If they did not, my application will display a User dialog with a login button. Otherwise, my application will display a dialog box . But when I click on my button, the User dialog is always displayed even in my Facebook account. Please help me fix this. Thanks in advance!;)

Additional information: AccessToken token = AccessToken.getCurrentAccessToken ();

+5
source share
1 answer

with a quick research, you need to introduce new methods to check the active session in facebook SDK 4.0, for example:

  • initialized your Facebok SDK (I think you already did it)
  • Deploy AccessTokenTracker

try to see this post , I think it can help you

0
source

All Articles