FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
callbackManager = CallbackManager.Factory.create();
setContentView(R.layout.activity_register_rs1);
//String username = getIntent().getExtras().getString("username");
//Log.w("username is",username);
loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
// App code
Log.d("FACEBOOK", "SUCCESS");
}
#Override
public void onCancel() {
// App code
Log.d("FACEBOOK", "CANCEL");
}
#Override
public void onError(FacebookException exception) {
// App code
Log.d("FACEBOOK", "ERROR"+exception.toString());
}
});
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
// App code
Log.d("FACEBOOK MANAGER", "SUCCESS");
}
#Override
public void onCancel() {
// App code
Log.d("FACEBOOK MANAGER", "CANCEL");
}
#Override
public void onError(FacebookException exception) {
// App code
Log.d("FACEBOOK MANAGER", "ERROR");
}
});
i use this code to Login with facebook and when i press to the facebook login button .. it's just stay loading and the FacebookActivity never show..
this is my logcat screen:
W/BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 8328
I opened an issue with Facebook for this problem - or at least what I think is the same issue.. basically in my case the login process is very slow and seems to hang for some users/devices.
If anyone has a similar issue can you add additional details to the bug below so we can get someone at FB to investigate?
https://developers.facebook.com/bugs/264734493940722/
Related
I want to ask the user of my android app the permission to publish on facebook when a custom share button (it's just an ImageView) is pressed. On button's OnClick method I execute this block:
CallbackManager facebookCallbackManager;
...
facebookCallbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(facebookCallbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
shareContent(activity, content, true);
}
#Override
public void onCancel() { }
#Override
public void onError(FacebookException error) { }
});
LoginManager.getInstance().logInWithPublishPermissions(activity, Collections.singletonList("publish_actions"));
And then I override:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebookCallbackManager.onActivityResult(requestCode, resultCode, data);
}
The problem is that the request never comes: an everlasting spinner wheel is presented and no callback (nor success, nor cancel, nor error) is called.
a. The user is already logged in, according to:
public static boolean isLoggedIn() {
AccessToken accessToken = AccessToken.getCurrentAccessToken();
return accessToken != null;
}
b. FacebookSdk.isInitialized() is true
c. Publish permissions are not granted, according to:
private static boolean hasPublishPermissions() {
AccessToken accessToken = AccessToken.getCurrentAccessToken();
return accessToken != null && accessToken.getPermissions().contains("publish_actions");
}
d. Other uses of FB SDK are used through the app and they are functioning.
e. I am an app admin on FB dashboard
Any idea on the problem?
Important PS:
Since Facebook's API is extremely stable, depending on the time of the day or the position of the stars, without changing code I have three possible outcomes:
As described before, an everlasting spinner wheel.
The callback fires the onCancel method without the user interaction.
It shares the content without asking for confirmation - this gave me a nice unwanted video posted on my personal FB, without me noticing :) -
PS2:
Even the classic LoginManager.getInstance().logInWithReadPermissionnow is having the same issue. It never had it before.
then you can share content without everlasting spinner
LoginManager.getInstance().registerCallback(mCallbackManagerFacebook, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
System.out.println("Success");
if (loginResult.getRecentlyGrantedPermissions().contains("publish_actions")) {
new shareContent(activity, content, true);
} else {
//Toast.makeText(youractivity.this, "Successfully Connected to Facebook.", Toast.LENGTH_LONG).show();
GetFacebookPublishPermissions();
}
}
#Override
public void onCancel() {
//Log.d(TAG_CANCEL, "On cancel");
LoginManager.getInstance().logOut();
}
#Override
public void onError(FacebookException error) {
Toast.makeText(youractivity.this, "Looks like we are unable to connect to Facebook Servers.", Toast.LENGTH_LONG).show();
LoginManager.getInstance().logOut();
// Log.d(TAG_ERROR, error.toString());
}
});
and getFacebookSharepermission
public void GetFacebookPublishPermissions(){
if(LoginManager.getInstance()!=null)
{
LoginManager.getInstance().logInWithPublishPermissions(youractivity.this, Arrays.asList("publish_actions"));
}
}
Update
you can call this function like below
AccessToken token = com.facebook.AccessToken.getCurrentAccessToken();
if(token!=null)
{
if (token.getPermissions().contains("publish_actions")) {
new shareContent(activity, content, true);
}else{
GetFacebookPublishPermissions();
}
}else{
LoginManager.getInstance().logInWithReadPermissions(this,
Arrays.asList("public_profile", "email", "user_birthday"));
}
I think there is mistake for callback manager.
you declare and assign CallbackManager facebookCallbackManager;
facebookCallbackManager = CallbackManager.Factory.create();
Here you can see there is cbManager instead of facebookCallbackManager
so please double check for that.
V
LoginManager.getInstance().registerCallback(cbManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
shareContent(activity, content, true);
}
#Override
public void onCancel() { }
#Override
public void onError(FacebookException error) { }
});
In my app, I am trying to implement Facebook login. I have an IntroLogin Activity after splash to determine whether the user is already logged in or not. If the user is already logged in, it straight loads MainActivity. Otherwise, it asks the user to log in.
My method to determine if the user is logged in or not works well in Android 6+. However, in Android Lollipop, after user sign-in (user get's to give app authenticate to use there info in popup), the app closes. I get no FCs or any messages in log. It just closes. Even if the user restarts the app, it again prompts to login.
My IntroLogin Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intro_login);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
loginButton = (Button) findViewById(R.id.login_button);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
AccessToken accessToken = AccessToken.getCurrentAccessToken();
if(accessToken != null){
finish();
}
else{
loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PopupLogin();
}
});
}
}
public void PopupLogin() {
// Set permissions
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile"));
LoginManager.getInstance().setLoginBehavior(LoginBehavior.WEB_VIEW_ONLY).registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Log.i(TAG, "onSuccess: " + loginResult);
startActivity(new Intent(IntroLogin.this, MainActivity.class));
finish();
}
#Override
public void onCancel() {
Log.d("MyApp", "Login canceled");
Snackbar snackbar = Snackbar.make(loginButton, "Login process canceled", Snackbar.LENGTH_INDEFINITE);
snackbar.getView().setBackgroundColor(ContextCompat.getColor(IntroLogin.this, R.color.colorPrimaryDark));
snackbar.setActionTextColor(ContextCompat.getColor(IntroLogin.this, R.color.pure_white));
snackbar.setAction(R.string.retry_login, new View.OnClickListener() {
#Override
public void onClick(View view) {
loginButton.performClick();
}
});
snackbar.show();
}
#Override
public void onError(FacebookException error) {
Log.d("Myapp", error.toString());
Toast.makeText(IntroLogin.this, error.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
UPDATE:
I found the cause but not the solution, I think OnActivityResult is not getting called in lollipop, that maybe the reason i am not seeing any log.
you should initialize your facebooksdk just after calling super.oncreate()
And also you are calling finish() when accessToken != null which means when user is already logged in facebook it will close the activity..
Change your onCreate() method code to:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_intro_login);
callbackManager = CallbackManager.Factory.create();
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
loginButton = (Button) findViewById(R.id.login_button);
updateWithToken(AccessToken.getCurrentAccessToken());
accessTokenTracker = new AccessTokenTracker() {
#Override
protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {
updateWithToken(newToken);
}
};
accessTokenTracker.startTracking();
}
private void updateWithToken(AccessToken currentAccessToken) {
if (currentAccessToken != null) {
Log.d("FB", "User Logged IN.");
//programetically logout user or do your on success
LoginManager.getInstance().logOut();
} else {
Log.d("FB", "User Logged Out.");
loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PopupLogin();
}
});
}
}
Found out the cause. I have added android:launchMode="singleTop" in my loginActivity, that was the reason why OnActivityResult was not calling.
I found Facebook Android SDK has deprecated its class Session in its 4.x version. What class or method I should use to replace Session.OpenRequest? Thanks.
The classes you need to look into are CallbackManager and LoginManager (and also AccessToken). I replaced my old Session.OpenRequest code with something akin to the following, it may be helpful as a starting point:
FacebookSdk.sdkInitialize(getApplicationContext());
m_CallbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(m_CallbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
completeLogin();
}
#Override
public void onCancel() {
// TODO: this
}
#Override
public void onError(FacebookException exception) {
// TODO: this
}
});
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends"));
I would like to get an Access Token for accessing the Graph for my Application, without using the Facebook.Login button. (The button is actually placed in the Action Bar).
Thanks in advance!
In onSuccess from LoginResult you can get AccessToken).
if (callbackManager == null) {
callbackManager = CallbackManager.Factory.create();
}
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
AccessToken.setCurrentAccessToken(loginResult.getAccessToken());
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
}
});
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends"));
I want to login Facebook SDK 4.3 with my own button but I'm facing some problems. Here is my code
public class MainActivity extends Activity {
CallbackManager callbackManager;
ShareDialog shareDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
callbackManager = CallbackManager.Factory.create();
Button bt= (Button)findViewById(R.id.button1);
shareDialog = new ShareDialog(this);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
openFB();
}
});
}
public void openFB() {
//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
}
}
I get this error at the last line:
The method logInWithReadPermissions(Fragment, Collection) in
the type LoginManager is not applicable for the arguments
(MainActivity, String)
As you know there is another logInWithReadPermissions method with 2 parameters are (Activity,Collection) but I don't know I get this error. Please help me to fix this!
For someone who encounter the same problem with me, just change the last line like this:
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends")); //Log in to FB
and it should be fine.