Android facebook Status on sharing complete or not - android

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() {
}
});
}

Related

Android Facebook parse login logic called twice

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.

Facebook authentication via android app

I am new to android. I am developing an application which requires facebook login to proceed. So for this I have followed this tutorial - https://developers.facebook.com/docs/android/getting-started
EDIT: Using UILifecycleHelper, same thing happen, once user authenticates the application application is closed.
private Session.StatusCallback callback = new Session.StatusCallback() {
#Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
Log.w("Vinit", "Session started");
// make request to the /me API
Request.newMeRequest(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
#Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
TextView t = (TextView) findViewById(R.id.textView1);
t.setText("User: " + user.getFirstName());
}
}
}).executeAsync();
}
}
};
private FacebookDialog.Callback dialogCallback = new FacebookDialog.Callback() {
#Override
public void onError(FacebookDialog.PendingCall pendingCall, Exception error, Bundle data) {
Log.d("HelloFacebook", String.format("Error: %s", error.toString()));
}
#Override
public void onComplete(FacebookDialog.PendingCall pendingCall, Bundle data) {
Log.d("HelloFacebook", "Success!");
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
if(new LoginChecker(this).isRegistered()){
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}else{
setContentView(R.layout.activity_login);
LoginButton loginButton = (LoginButton) findViewById(R.id.loginButton1);
thisActivity = this;
loginButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
uiHelper = new UiLifecycleHelper(thisActivity, callback);
}
});
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.w("Vinit", "On Activity Result function");
uiHelper.onActivityResult(requestCode, resultCode, data, dialogCallback);
}
Use UiLifecycleHelper
https://developers.facebook.com/docs/reference/android/current/class/UiLifecycleHelper
to be sure that your activity/fragment handle processing from facebook component in proper way.
Check Facebook Session object.
Does app call instructions inside session.IsOpened()? Try debug your code to see state of the Facebook Session.

Facebook ShareDialog: back button doesn't catch by Activity

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);
}

Android FB Connect

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

Issue with Android Facebook SDK

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);
}

Categories

Resources