I have done facebook login check in my android app using LoginButton, but I want to check using LoginManager. How can I modify my code to work that 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 Facebook login dialog if user is not logged in before opening by Share dialog. The Login dialog should be same as Share Dialog (in a popup). How to do it?
Thanks!
You can check, if user logged, by calling the method Loginmanger.logInWithReadPermissions, to manually start login process, without button. You can do it in the onCreate method. If user are logged, it immediately call onSuccess callback method, where you can call your shareDialog. If user are not logged, it will show login screen. And if it was successful (i.e user logged), onSuccess method will be called;
Actually, you can remove from your code all login button mentions, and add one line to the end:
LoginManager.getInstance().logInWithReadPermissions(this, "user_friends"); //Log in to FB
You can use also use WritePermissions. You can read about required permissions here.
Here's your code, but wrapped as login method, without any login button instances. You can call it anywhere, when you need to check, if user is logged.
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
}
You can get the User Profile for that , Try this ,
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 callback for above request
You can use ProfileTracker in a similar way 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
来源:https://stackoverflow.com/questions/29739428/android-facebook-login-how-to-check-login-using-loginmanager-without-having-lo