I'm creating an Android App that ask the user for this permissions: Default Login permission + user_friends + publish_action.
In my first activity i did the Login + ask the "user_friends" permission and work great.
Now in my Second Activity i want to ask for the "publish_action" permission after a AlertDialog choice.
The problem is that i never been promped for the "publish_action" box, the app skip the request and go ahead calling another Activity (Leaderboards)
Here is my Second Activity:
CallbackManager callbackManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);
callbackManager = CallbackManager.Factory.create();
final Context context = this;
//stuff
b_exit.setOnClickListener(new View.OnClickListener()
{
public void onClick(View V)
{
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(getString(R.string.salva_dati))
.setCancelable(true)
.setPositiveButton(getString(R.string.si), new DialogInterface.OnClickListener() {
public void onClick(#SuppressWarnings("unused") final DialogInterface dialog, #SuppressWarnings("unused") final int id) {
askForFBPublishPerm();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.clear();
editor.apply();
Intent intent = new Intent(getApplicationContext(), Leaderboard.class);
startActivity(intent); // IT START WITHOUT ASK ME THE PERMISSION
}
}
}
})
}
void askForFBPublishPerm(){
Log.d("perm", "asking for the permissions"); //I SEE THAT LOG BUT NOTHING MORE AFTER THAT LINE
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().logInWithPublishPermissions(this,Arrays.asList("publish_actions"));
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Log.d("perm", "SUCCESS");//I DONT SEE THAT LOG
}
#Override
public void onCancel() {
Toast.makeText(getApplicationContext(), "cancelled while asking publish permission", Toast.LENGTH_LONG).show();
Log.d("perm", "cancelled while asking publish permission");
//I DONT SEE THAT LOG
}
#Override
public void onError(FacebookException error) {
Toast.makeText(getApplicationContext(), "error occurred while asking publish permission!", Toast.LENGTH_LONG).show();
Log.d("perm", "error occurred while asking publish permission!"); //I DONT SEE THAT LOG
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
In my Log i see that askForFBPublishPerm() as been called but it never enter in onSuccess() or onCancel() or onError().
I also get reviewed my app by Facebook and they gave me the "publish_action" permission.
This "publish_action" request dont work and i dont know why, someone can help me? Thanks in advice
Handle your callbackManager in onActivityResult. Just apply this code.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data); // this line is required
}
Related
I'm new in Android programming and I don't know how I can put the facebook logout botton in another activity (called whatToDo) considering that the login botton is in the LoginActivity.
This is the initial code of Facebook tutorial to implement Facebook login, but from this code I don't know how to implement what I want.
public class LoginActivity extends Activity {
private CallbackManager callbackManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
callbackManager = CallbackManager.Factory.create();
TextView login_text = (TextView) findViewById(R.id.fb_login);
LoginButton fbLoginButton = (LoginButton) findViewById(R.id.fb_login_button);
fbLoginButton.setReadPermissions("email");
fbLoginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Intent intent = new Intent(LoginActivity.this, whatToDo.class);
startActivity(intent);
Toast.makeText(getApplicationContext(), "Logged!", Toast.LENGTH_LONG).show();
}
#Override
public void onCancel() {
Toast.makeText(getApplicationContext(), "Cancel Event!", Toast.LENGTH_LONG).show();
}
#Override
public void onError(FacebookException exception) {
Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
}
}
What extra code do I have to insert in LoginActivity and whatToDo activity?
Thanks in advance!
So here is the code
public void redirectToMain(){
Intent intent = new Intent(getApplicationContext(), MainScreen.class);
startActivity(intent);
finish();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pers.clear();
pers.add("user_friends");
pers.add("email");
if (ParseUser.getCurrentUser() != null) {
redirectToMain();
}
loginButton = (LoginButton) findViewById(R.id.login_button);
callbackManager = CallbackManager.Factory.create();
loginButton.setReadPermissions("email", "user_friends");
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
parseLogin();
}
#Override
public void onCancel() {
// App code
}
#Override
public void onError(FacebookException exception) {
// App code
}
});
}
public void parseLogin(){
loginButton.setVisibility(View.INVISIBLE);
ParseFacebookUtils.logInWithReadPermissionsInBackground(this, pers, new LogInCallback() {
#Override
public void done(ParseUser user, ParseException e) {
if(user==null){
Log.i("Cancel", "facebook");
}else if(user.isNew()){
Log.i("Parse", "succcess");
redirectToMain();
}
else{
Log.i("Parse", "old");
redirectToMain();
}
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data );
ParseFacebookUtils.onActivityResult(requestCode,resultCode,data);
}
}
And everything works fine, the user gets redirected to the second activity, but the activity gets called twice. Also, the log part that says old is also written twice in the logcat, so my assumption is that either parseLogin() or the ParseFacebookUtils.log...() is called twice. Any idea why that is happening?
I managed to fix it by adding an int and a while loop that executes the code only once. It's more of a hack than a fix, so maybe somebody can come up with a better solution.
Here is how I implemented callback mechanism for FB login button:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
setContentView(R.layout.activity_main);
loginButton = (LoginButton)findViewById(R.id.login_button);
callbackManager = CallbackManager.Factory.create();
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Log.d("FB onSuccess 1", "");
}
#Override
public void onCancel() {
Log.d("FB onCancel 1", "");
}
#Override
public void onError(FacebookException e) {
Log.d("FB onError 1", "");
}
});
}
When pressing button a spinner appear, starts, but the confirmation screen does not appear, and no log messages are filled into Activity Monitor. What is the problem?
Try Adding this in your Activity :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
The facebook login is showing, I logged into facebook, confirmed the information, but when I go back to my app the AccessToken did not work. When I tested:
FacebookSdk.sdkInitialize(getApplicationContext(), new FacebookSdk.InitializeCallback() {
#Override
public void onInitialized() {
if(AccessToken.getCurrentAccessToken() == null){
System.out.println("not logged in yet");
} else {
System.out.println("Logged in");
}
}
});
the result is not logged in yet
This is the code login:
public void loginFacebook(View v){
LoginManager.getInstance().logInWithPublishPermissions(this, Arrays.asList("publish_actions"));
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Toast.makeText(UserPerfilActivity.this, "VocĂȘ Clicou em: ", Toast.LENGTH_LONG).show();
System.out.println("publish");
// sharePhotoToFacebook();
}
#Override
public void onCancel() {
System.out.println("onCancel");
}
#Override
public void onError(FacebookException exception) {
System.out.println("onError");
}
});
}
But the callbackManager doesn't work, I don't have publish or cancel or error println.
Fix the problem, I added this code:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode,
resultCode, data);
}
this is the post:
post stackoverflow link
I am trying to integrate the official FB SDK for android.
below is the basic code
public class FbTestActivity extends Activity {
Facebook facebook = new Facebook("112374492201730");
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
facebook.authorize(this, new String[] { "email", "read_stream" },
new DialogListener() {
public void onComplete(Bundle values) {
}
public void onFacebookError(FacebookError error) {
}
public void onError(DialogError e) {
}
public void onCancel() {
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
}
When i open the app the Login or (if looged in) Permission dialog never shows up. Some dialog opens for a few mili secs and closes down.
The problem is the SSO (Single Sign On) feature of the login.
Following link will help you.
facebook login dialog disappears soon after loading
http://ukgupta.blogspot.com/2011/07/facebook-implementation-into-android.html