When I'm connected by the facebook login button, I launch a custom dialog (in the onSuccess) and I want to write in this dialog the name of the person connected. It works when I do it in the layout called in onCreate(I make appear the name after connexion). But it doesn't work when I want to write this in the layout called by the dialog after the connexion (but it's the same activity).
Here is my code :
private CallbackManager mCallbackManager2;
private ProfileTracker mProfileTracker2;
private LoginButton loginbutton2;
final Context context = this;
private ProfilePictureView fAvatar2;
private TextView fName2;
private FacebookCallback<LoginResult> mCallback2 = new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
/*
LAUNCH DIALOG ABOUT FACEBOOK CONNEXION
*/
// custom dialog
final Dialog dialogFacebookConnexion = new Dialog(context);
dialogFacebookConnexion.setContentView(R.layout.dialog_facebook_connect);
Button dialogButton = (Button) dialogFacebookConnexion.findViewById(R.id.dialogOk);
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialogFacebookConnexion.dismiss();
Intent intentmainpage = new Intent(InfoAccountActivity.this, MainPageActivity.class);
startActivity(intentmainpage);
}
});
dialogFacebookConnexion.setCanceledOnTouchOutside(false);
int dialogHeight = (int) getResources().getDimension(R.dimen.dialog_height);
int dialogWidth = (int) getResources().getDimension(R.dimen.dialog_width);
dialogFacebookConnexion.getWindow().setLayout(dialogWidth, dialogHeight);
dialogFacebookConnexion.show();
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_info_account);
mCallbackManager2 = CallbackManager.Factory.create();
loginbutton2 = (LoginButton) findViewById(R.id.login_button2);
loginbutton2.setBackgroundResource(R.drawable.facebookconnexion);
loginbutton2.setReadPermissions("user_friends");
loginbutton2.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
loginbutton2.registerCallback(mCallbackManager2, mCallback2);
fName2 = (TextView) findViewById(R.id.facebookName2);
fAvatar2 = (ProfilePictureView) findViewById(R.id.facebook_avatar2);
ProfileTracker profileTracker = new ProfileTracker() {
#Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) {
OnProfileChanged(newProfile);
}
};
}
private void OnProfileChanged (Profile profile) {
if (profile != null) {
try {
fName2.setText(profile.getName());
fAvatar2.setProfileId(profile.getId());
} catch (Exception ex) {
}
} else {
}
}
#Override
public void onResume() {
super.onResume();
Profile profile = Profile.getCurrentProfile();
OnProfileChanged(profile);
}
#Override
public void onStop() {
super.onStop();
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mCallbackManager2.onActivityResult(requestCode, resultCode, data);
}
Is it a problem with private or protected ? An information can't be access by the dialog?
In any activity, you can use the following facebook code from their SDK once you have signed into Facebook:
Profile.getCurrentProfile().getName()
Did you try using the Request.newMeRequest using your active open session? It is easy to capture the logged user's profile afterwards.
GraphRequest rq = GraphRequest.newMeRequest(session, new Request.GraphUserCallback() {
#Override
public void onCompleted(final GraphUser user, Response response) { ...
...
rq.executeAndWait();
You can retrieve the user's name by user.getName() in the onCompleted method and use it anywhere.
Related
I managed to get the first_name, last_name and the link_uri. I am not sure how to get the user's date of birth.
This is the code I used in my loginActivity:
public class LoginActivity extends AppCompatActivity {
private CallbackManager callbackManager;
private AccessTokenTracker accessTokenTracker;
private ProfileTracker profileTracker;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_login);
callbackManager = CallbackManager.Factory.create();
accessTokenTracker = new AccessTokenTracker() {
#Override
protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {
}
};
profileTracker = new ProfileTracker() {
#Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) {
nextActivity(newProfile);
}
};
accessTokenTracker.startTracking();
profileTracker.startTracking();
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Profile profile = Profile.getCurrentProfile();
nextActivity(profile);
Toast.makeText(getApplicationContext(), "Logging in...", Toast.LENGTH_SHORT).show();
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
}
};
loginButton.setReadPermissions("user_friends");
loginButton.registerCallback(callbackManager, callback);
}
#Override
protected void onResume() {
super.onResume();
//Facebook login
Profile profile = Profile.getCurrentProfile();
nextActivity(profile);
}
#Override
protected void onPause() {
super.onPause();
}
protected void onStop() {
super.onStop();
//Facebook login
accessTokenTracker.stopTracking();
profileTracker.stopTracking();
}
#Override
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
super.onActivityResult(requestCode, responseCode, intent);
//Facebook login
callbackManager.onActivityResult(requestCode, responseCode, intent);
}
private void nextActivity(Profile profile) {
if (profile != null) {
Intent main = new Intent(LoginActivity.this, MainActivity.class);
main.putExtra("name", profile.getFirstName());
main.putExtra("surname", profile.getLastName());
main.putExtra("imageUrl", profile.getProfilePictureUri(200, 200).toString());
startActivity(main);
}
}
}
I want to show the age of the user in the next activity so do I also need to do something with the returned value? (I assume it would be something like dd/mm/yyyy or mm/dd/yyyy)
you have to submit you application to review on facebook developer console or
create test developer for the app. and for those account ask for permission of "date of birth" while running code, you will get dob for those test developer
I'm trying to implement the Facebook Login on an Android app. I followed the Facebook Developer guide and it's working but if Facebook app is not installed then it takes login details,logs in and stays on same activity and just show logout button, if I close application and start again then it works perfectly fine.
public class MainActivity extends AppCompatActivity {
LoginButton loginButton;
CallbackManager callbackManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
FacebookSdk.sdkInitialize(getApplicationContext());
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
isLoggedIn();
loginButton = (LoginButton) findViewById(R.id.login_button);
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
nextActivity(profile);
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException error) {
}
});
loginButton.setReadPermissions("user_friends");
}
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
super.onActivityResult(requestCode, responseCode, intent);
//Facebook login
callbackManager.onActivityResult(requestCode, responseCode, intent);
}
public void isLoggedIn() {
AccessToken accessToken = AccessToken.getCurrentAccessToken();
if (accessToken!=null) {
Profile profile=Profile.getCurrentProfile();
nextActivity(profile);
}
}
private void nextActivity(Profile profile) {
if (profile != null) { Intent main = new Intent(MainActivity.this, Result.class);
main.putExtra("Name", profile.getFirstName());
main.putExtra("Surname", profile.getLastName());
startActivity(main);
finish();
}
}
}
Instead of using LoginManager.getInstance().registerCallback() use loginButton.registerCallback()
EDIT:
Inside onSuccess have following code:
#Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
if(Profile.getCurrentProfile() == null) {
ProfileTracker profileTracker = new ProfileTracker() {
#Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
this.stopTracking();
nextActivity(currentProfile);
}
};
profileTracker.startTracking();
}else{
Profile profile = Profile.getCurrentProfile();
nextActivity(profile);
}
so i created a simple facebook login app following the exact instructions from this video: https://www.youtube.com/watch?v=myWu-q8Q2NA&list=PLonJJ3BVjZW4E1wIRZvuXhbFRGWB1grmh
anyways i am able to sign in but unlike the video, when I sign in, I appear back at the login page, the login button does not change to the logout button,
I click the login button, enter my login details, hit send, then a message says: You have already authorized "LoginExample (app name)". Then I appear back at the login with facebook button.
and on a similar note, I am trying to get a TextView to display a "Welcome", however it does not work, it only works when I do ' android:text="Welcome" '.
Below is my code in a fragment, I do setContentView(activity_fragment); in MainActivity. Again, the Login button appears but the TextView does not.
What is going wrong?
public class MainFragment extends Fragment {
public TextView mTextDetails;
private AccessTokenTracker mtokentracker;
private ProfileTracker mprofiletracker;
CallbackManager mCallBackManager;
private FacebookCallback<LoginResult> mCallBack = new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
//TextView mTextDetails = (TextView)getView().findViewById(R.id.textView);
Profile profile = Profile.getCurrentProfile();
if (profile != null){
mTextDetails.setText("Welcome " + profile.getName());
}
//setContentView(R.layout.activity_main);
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
}
};
public MainFragment() {
//TextView mTextDetails = (TextView) getView().findViewById(R.id.textView);
//mTextDetails.setText("Welcome");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
mCallBackManager = CallbackManager.Factory.create();
AccessTokenTracker tracker = new AccessTokenTracker() {
#Override
protected void onCurrentAccessTokenChanged(AccessToken old, AccessToken niw) {
Profile profile = Profile.getCurrentProfile();
if (profile != null){
mTextDetails.setText("welcome " + profile.getName());
}
}
};
ProfileTracker profileTracker = new ProfileTracker() {
#Override
protected void onCurrentProfileChanged(Profile old, Profile niw) {
}
};
mtokentracker.startTracking();
mprofiletracker.startTracking();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
LoginButton loginButton = (LoginButton)view.findViewById(R.id.login_button);
//loginButton.setReadPermissions("user_friends");
loginButton.setFragment(this);
loginButton.registerCallback(mCallBackManager, mCallBack);
TextView mTextDetails = (TextView)getView().findViewById(R.id.textView);
mTextDetails.setText("Welcome");
}
#Override
public void onResume() {
super.onResume();
Profile profile = Profile.getCurrentProfile();
if (profile != null){
mTextDetails.setText("Welcome " + profile.getName());
}
}
#Override
public void onStop() {
super.onStop();
mtokentracker.stopTracking();
mprofiletracker.stopTracking();
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mCallBackManager.onActivityResult(requestCode,resultCode,data);
}
}
I am new to android development. I am trying to implement login through facebook using facebook SDK 4.0.0.
Login is successful but it stays in the same activity for a second, the button changes to Logout and then the second activity appears. I want it to directly move to second activity.
I searched for it but I couldn't figure it out.
(The viewflipper is calling next layout which is just a progress bar)
Following is my code.
public class MainActivity extends ActionBarActivity {
private String name;
private String lname;
private LoginButton loginButton;
private ProfileTracker mProfileTracker;
private CallbackManager mCallBackManager;
private AccessTokenTracker mAccessTracker;
SharedPreferences sp;
private FacebookCallback<LoginResult> mCallBack = new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
AccessToken atoken = loginResult.getAccessToken();
ViewFlipper vp = (ViewFlipper) findViewById(R.id.flip);
vp.showNext();
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
}
};;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sp = getPreferences(MODE_PRIVATE);
if (sp.contains("username")) {
Intent i = new Intent(this, userSelect.class);
i.putExtra("username", sp.getString("username", "noUser"));
i.putExtra("lname", sp.getString("lastname", "nomail"));
startActivity(i);
}
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);
loginButton = (LoginButton)findViewById(R.id.login_button);
loginButton.setReadPermissions("email");
mCallBackManager = CallbackManager.Factory.create();
loginButton.registerCallback(mCallBackManager, mCallBack);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (sp.contains("username")) {
Intent i = new Intent(this, userSelect.class);
i.putExtra("username", sp.getString("username", "noUser"));
i.putExtra("lname", sp.getString("lastname", "nomail"));
startActivity(i);
}
mCallBackManager.onActivityResult(requestCode, resultCode, data);
mAccessTracker = new AccessTokenTracker() {
#Override
protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {
}
};
mProfileTracker = new ProfileTracker() {
#Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) {
if ( newProfile != null)
{
name = newProfile.getFirstName();
lname = newProfile.getLastName();
Intent intent = new Intent(MainActivity.this, userSelect.class);
Editor editor = sp.edit();
editor.putString("username", name);
editor.putString("lastname",lname );
editor.commit();
intent.putExtra("username", name);
intent.putExtra("lname",lname);
startActivity(intent);
}
}
};
mAccessTracker.startTracking();
mProfileTracker.startTracking();
}
}
You have to call new activity in your onSuccess method. By this your activity is called only after user logged in Successfully.
private FacebookCallback<LoginResult> mCallBack= new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
//call new activity here.
}
I have the follow code, but I don't know which is the method that execute when I push in Logout. I only need know which is the method that execute in logout. Or another solution? Thanks for all :) dasasa
public class FBLoginFragment extends Fragment implements IServiceCallback {
private CallbackManager callbackManager;
private TextView textView;
private AccessTokenTracker accessTokenTracker;
private ProfileTracker mProfileTracker;
private FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
mProfileTracker = new ProfileTracker() {
#Override
protected void onCurrentProfileChanged(Profile profile, Profile profile2) {
mProfileTracker.stopTracking();
}
};
mProfileTracker.startTracking();
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
displayMessage(profile);
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
}
};
public FBLoginFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
callbackManager = CallbackManager.Factory.create();
accessTokenTracker= new AccessTokenTracker() {
#Override
protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {
}
};
mProfileTracker = new ProfileTracker() {
#Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) {
displayMessage(newProfile);
if(newProfile != null){
register(newProfile);
}
}
};
accessTokenTracker.startTracking();
mProfileTracker.startTracking();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.facebook_fragment, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
textView = (TextView) view.findViewById(R.id.textView);
loginButton.setReadPermissions(Arrays.asList("public_profile", "user_friends"));
loginButton.setFragment(this);
loginButton.registerCallback(callbackManager, callback);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (callbackManager.onActivityResult(requestCode, resultCode, data))
return;
}
private void displayMessage(Profile profile){
if(profile != null){
textView.setText(profile.getName());
}
}
private void register(Profile profile){
if(profile != null){
new Register(this).run(profile);
}
}
#Override
public void onStop() {
super.onStop();
accessTokenTracker.stopTracking();
mProfileTracker.stopTracking();
}
#Override
public void onResume() {
super.onResume();
Profile profile = Profile.getCurrentProfile();
displayMessage(profile);
}
#Override
public void onSuccess(JSONObject obj, String method) throws JSONException {
User user = new User(obj);
user.insert();
//Intent intent = new Intent(getActivity(), Update.class);
//startActivity(intent);
}
#Override
public void onError(String error) {
}
You need to get the active Session, then then call closeAndClearTokenInformation().
You can get the currently active session via Session.getActiveSession().
Try this method to logout from facebook programmatically in android
/**
* Logout From Facebook
*/
public static void callFacebookLogout(Context context) {
Session session = Session.getActiveSession();
if (session != null) {
if (!session.isClosed()) {
session.closeAndClearTokenInformation();
//clear your preferences if saved
}
} else {
session = new Session(context);
Session.setActiveSession(session);
session.closeAndClearTokenInformation();
//clear your preferences if saved
}
}