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
Related
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
}
I am using latest Facebook SDK for sharing link. I am using standard code that is given in the tutorial. But so far I am unable to know the status of post.
i.e. whether user posts through Facebook dialog or cancelled it I always receive null/same values.
As it is important part of my application logic. I want to know whether user really posted or not.
Code I am using in activity result:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("hariss",resultCode+"");
uiHelper.onActivityResult(requestCode, resultCode, data, new FacebookDialog.Callback() {
#Override
public void onError(FacebookDialog.PendingCall pendingCall, Exception error, Bundle data) {
Log.e("Activity", String.format("Error: %s", error.toString()));
}
#Override
public void onComplete(FacebookDialog.PendingCall pendingCall, Bundle data) {
Log.i("Activity", "Success!");
//tried various things here to know weather user posted or not, but failed
}
});
}
Use the below method for posting and you can do the later task in onComplete method.
public void postToWall() {
// post on user's wall.
facebook.dialog(this, "feed", new DialogListener() {
#Override
public void onFacebookError(FacebookError e) {
}
#Override
public void onError(DialogError e) {
}
#Override //enter code here
public void onComplete(Bundle values) {
}
#Override
public void onCancel() {
}
});
}
I follow tutorial for write ShareDialog of facebook api, but it doesn't work. When activity is launched, after setting up environment, i show automatically ShareDialog. If i press back button when that dialog is showed, my app close dialog, and open it again. It seems that back button isn't catch by activity. This is my code:
public class ShareScoreOnFb extends FragmentActivity {
private static UiLifecycleHelper uiHelper;
private static Activity activity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share_score_on_fb);
activity = this;
uiHelper = new UiLifecycleHelper(this, null);
uiHelper.onCreate(savedInstanceState);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data,
new FacebookDialog.Callback() {
#Override
public void onComplete(PendingCall pendingCall, Bundle data) {
String gesture = FacebookDialog
.getNativeDialogCompletionGesture(data);
if (gesture != null) {
if ("post".equals(gesture)) {
Toast.makeText(ShareScoreOnFb.this,
"success",
Toast.LENGTH_SHORT).show();
}
ShareScoreOnFb.this.finish();
}
ShareScoreOnFb.this.finish();
}
#Override
public void onError(PendingCall pendingCall,
Exception error, Bundle data) {
Toast.makeText(ShareScoreOnFb.this, error.toString(),
Toast.LENGTH_SHORT).show();
ShareScoreOnFb.this.finish();
}
});
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
#Override
protected void onResume() {
super.onResume();
uiHelper.onResume();
share();
}
#Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
#Override
public void onBackPressed() {
Log.i("TAG", "backpressed");
super.onBackPressed();
}
#Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
private void share() {
Intent intent = this.getIntent();
int myScore = intent.getIntExtra("MYSCORE", -1);
int oppScore = intent.getIntExtra("OPPONENT_SCORE", -1);
FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder(this)
.setName("Score").setLink("http://www.google.it")
.setDescription(myScore + " " + oppScore)
.setPicture("http://www.cs.cmu.edu/~junggon/tools/gear.png")
.build();
uiHelper.trackPendingDialogCall(shareDialog.present());
}
}
I can't find any solution, and i'm going to be crazy!
When you call the native share dialog (in FacebookDialog), it will start another activity (call it Activity Foo) in the Facebook app. So if you click on the back button, it's Activity Foo that catches the back button, and finishes Activity Foo. I believe here's the sequence of events:
Your ShareScoreOnFb activity calls shareDialog.present()
Activity Foo starts, and shows the share dialog
You click back
Activity Foo finishes, and calls your callback (in onActivityResult)
once onActivityResult completes, your ShareScoreOnFb activity's onResume is called, and it will try to share() again
This will handle result without back press.
I have tried this and got success.
On Click (For Link Sample (You can modify your content)) :
shareDialog = new ShareDialog(MainActivity.this);
ShareLinkContent linkContent = new ShareLinkContent.Builder()
.setContentTitle("Hello Facebook")
.setContentDescription("The 'Hello Facebook' sample showcases simple Facebook integration")
.setContentUrl(Uri.parse("http://developers.facebook.com/android"))
.build();
shareDialog.show(linkContent, ShareDialog.Mode.AUTOMATIC);
In onActivityResult method :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
I am trying to integrate Facebook ** into my android app. I want to implement **like and share ** facility on **facebook. But i am getting error.
please help me to solve this.
Also I want to know the process of creating New App on Facebook.
I am using code from GitHub. My code is -
public class MyGreatActivity extends Activity {
Facebook facebook = new Facebook("333778590046892");
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
facebook.authorize(this, new String[] { "offline_access", "publish_stream", "raj21kadam#gmail.com" },
new DialogListener() {
#Override
public void onComplete(Bundle values) {
String token=facebook.getAccessToken(); //get access token
// Toast.makeText(this, "token", Toast.LENGTH_LONG).
save(token);
}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
private void save(String token){
Toast.makeText(this, "token"+token, Toast.LENGTH_LONG).show();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putString("Token", token).commit();
}
}
I am getting the following error while running the above code -
Dialog Errorcom.facebook.android.DialogError: The connection to the server was unsuccessful.
try this Single Sign On (SSO) Using Android Native Client For Facebook .
paste this
facebook.authorize(this, new DialogListener() {
#Override
public void onComplete(Bundle values) {
String token=facebook.getAccessToken(); //get access token
// Toast.makeText(this, "token", Toast.LENGTH_LONG).
save(token);
}
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
instead of this
facebook.authorize(this, new String[] { "offline_access", "publish_stream", "raj21kadam#gmail.com" },
new DialogListener() {
#Override
public void onComplete(Bundle values) {
String token=facebook.getAccessToken(); //get access token
}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
in my activity I use the following code to get Facebook authorization. facebook.authorize is called but it does not enter in one of the listener event (onComplete, onFacebookError...). Anybody could help?
Facebook facebook = new Facebook(FACEBOOK_APPID);
facebook.authorize(ItemDetail.this, new String[] {"publish_stream"},
new DialogListener() {
#Override
public void onComplete(Bundle values) {
Log.i("ItemDetail", "Facebook onComplete");
}
#Override
public void onFacebookError(FacebookError error) {
Log.i("ItemDetail", "Facebook onFacebookError");
}
#Override
public void onError(DialogError e) {
Log.i("ItemDetail", "Facebook onError");
}
#Override
public void onCancel() {
Log.i("ItemDetail", "Facebook onCancel");
}
}
);
You need to put this in your class:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
facebook.authorizeCallback(requestCode, resultCode, data);
}