Android Facebook Login: how to validate login with LoginManager without login in XML format
I have done facebook login validation in my android app using LoginButton, but I want to validate using LoginManager. How can I change my code to work this way?
LoginButton loginButton = (LoginButton) findViewById(R.id.fb_login_button);
loginButton.setReadPermissions("user_friends");
shareDialog = new ShareDialog(this);
//Login Callback registration
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Toast.makeText(getApplicationContext(), "in LoginResult on success", Toast.LENGTH_LONG).show();
//Login success - process to Post
if (ShareDialog.canShow(ShareLinkContent.class)) {
String description = "description";
ShareLinkContent linkContent = new ShareLinkContent.Builder()
.setContentTitle("title")
.setContentDescription(description)
.setContentUrl(Uri.parse("http://google.com"))
.setImageUrl(Uri.parse("http://google.com"))
.build();
shareDialog.show(linkContent, ShareDialog.Mode.FEED);
}
}
@Override
public void onCancel() {
Toast.makeText(getApplicationContext(), "in LoginResult on cancel", Toast.LENGTH_LONG).show();
}
@Override
public void onError(FacebookException exception) {
Toast.makeText(getApplicationContext(), "in LoginResult on error", Toast.LENGTH_LONG).show();
}
});
Also I want to open the Facebook login dialog if the user is not logged in before opening the Share dialog. The Login dialog must match the Shared dialog (pop-up). How to do it?
Thank!
source to share
You can check if the user is logged in by calling the method Loginmanger
. logInWithReadPermissions to manually start the login process, without a button, you can do it in the method onCreate
. If the user is logged in, it immediately invokes the callback method onSuccess
where you can call yours shareDialog
. If the user is not registered, it will display the login screen. And if it was successful (for example, the user logged in), the method will be called onSuccess
;
Actually, you can remove all login button
mentions from your code and add one line at the end:
LoginManager.getInstance().logInWithReadPermissions(this, "user_friends"); //Log in to FB
You can also use WritePermissions. You can read about the required permissions here .
Here's your code, but completed as a login method, without any instances of the login button. You can call him anywhere when you need to check if the user is registered.
public void checkLogin() {
//Login Callback registration
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Toast.makeText(getApplicationContext(), "in LoginResult on success", Toast.LENGTH_LONG).show();
//Login success - process to Post
if (ShareDialog.canShow(ShareLinkContent.class)) {
String description = "description";
ShareLinkContent linkContent = new ShareLinkContent.Builder()
.setContentTitle("title")
.setContentDescription(description)
.setContentUrl(Uri.parse("http://google.com"))
.setImageUrl(Uri.parse("http://google.com"))
.build();
shareDialog.show(linkContent, ShareDialog.Mode.FEED);
}
}
@Override
public void onCancel() {
Toast.makeText(getApplicationContext(), "in LoginResult on cancel", Toast.LENGTH_LONG).show();
}
@Override
public void onError(FacebookException exception) {
Toast.makeText(getApplicationContext(), "in LoginResult on error", Toast.LENGTH_LONG).show();
}
});
LoginManager.getInstance().logInWithReadPermissions(this, "user_friends"); //Log in to FB
}
source to share
You can get user profile for this, try,
Profile dd = Profile.getCurrentProfile().getCurrentProfile();
if(dd != null)
{
//user has logged
}
else
{
//not logged
}
You can re-initiate login without LoginButton as follows,
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList(<required permissions>));
Also you need to implement a callback for the above request
source to share
You can use ProfileTracker in the same way as to track changes in the current profile:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this.getApplicationContext());
callbackManager = CallbackManager.Factory.create();
profileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(
Profile oldProfile,
Profile currentProfile) {
// here if session exist
}
};
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onDestroy() {
super.onDestroy();
profileTracker.stopTracking();
}
Source from here: https://developers.facebook.com/docs/facebook-login/android/v2.3#get_current
source to share