How to replace Facebook Android SDK replace Session.OpenRequest - android

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

Related

Android FacebookActivity doesn't show Cannot call determinedVisibility()

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/

Android facebook api

i'm just trying to use the facebook api in my android application, my code is :
public class FacebookActivity extends AppCompatActivity{
CallbackManager callbackManager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_facebook);
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
callbackManager = CallbackManager.Factory.create();
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
}
});
I think this is the base but i don't know how to do it properly.
I have a button to connect to my facebook accompte, but in my method onSucces, i don't know what i have to put in.
My second question is about request. I want to integrate a friend request in this same app but i don't found any informations if someone can help me.
Thanks you
<com.facebook.login.widget.LoginButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
facebook:com_facebook_confirm_logout="false"
facebook:com_facebook_tooltip_mode="never_display"/>
Used this in your xml file

Android Studio: Facebook get access Tokken

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

How can I login Facebook SDK 4.3 with my own button?

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.

Facebook SDK for Android Cannot Resolve CallbackManager.registerCallback

I have been working to get Facebook SDK 4.0.1 running on Android but I have not been able to get the LoginButton to cause a login screen to open.
In the XML for my screen I have:
<com.facebook.login.widget.LoginButton
android:id="#+id/myLoginButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
/>
Then in my Activity I have:
CallbackManager callbackManager;
LoginButton loginButton;
#Override
public void onCreate(Bundle savedInstanceState) {
MainActivity.localyticsSession.tagEvent("Sign In");
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.app_user_loginactivity);
callbackManager = CallbackManager.Factory.create();
// Initialize the Facebook LoginButton
loginButton = (LoginButton) findViewById(R.id.myLoginButton);
//loginButton.setBackgroundColor(Color.RED); // this works
loginButton.setReadPermissions("public_profile");
// Callback registration
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
// App code
Log.i("LoginActivity","FB Login Success");
}
#Override
public void onCancel() {
// App code
Log.i("LoginActivity","FB Login Cancel");
}
#Override
public void onError(FacebookException exception) {
// App code
Log.i("LoginActivity","FB Login Error");
}
});
}
When I tap the LoginButton it blinks like it is reacting, but the login screen does not appear.
Update
Android Studio is giving me a message that says Cannot resolve method registerCallback. I looked in com.facebook.CallbackManager which then imports com.facebook.internal.CallbackManagerImpl
CallbackManagerImpl.java contains this method:
public void registerCallback(int requestCode, Callback callback) {
Validate.notNull(callback, "callback");
callbacks.put(requestCode, callback);
}
I added callbackManager = CallbackManager.Factory.create(); to my code, but that is not making a difference.
where is the "onClick()" implementation for the FB logon button?
if its in the widget class then you need to drill-down into what is being called on the button click event....
below is sample from a more complex example (git parselogin-ui) but it gives the idea of what u are missing...
facebookLoginButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
loadingStart(false); // Facebook login pop-up already has a spinner
if (config.isFacebookLoginNeedPublishPermissions()) {
ParseFacebookUtils.logInWithPublishPermissionsInBackground(getActivity(),
config.getFacebookLoginPermissions(), facebookLoginCallbackV4);
} else {
ParseFacebookUtils.logInWithReadPermissionsInBackground(getActivity(),
config.getFacebookLoginPermissions(), facebookLoginCallbackV4);
}
}
});
see here #292

Categories

Resources