kuldeep raj kuldeep raj - 8 days ago 5
Android Question

Login with facebook sdk shared preference and get information about user

i am developing an app where facebook login is there . i am trying to get user information and send that to my server.i am using sdk 4.i am trying to use shared preference to save info but i have no idea about shared preference . This is my facebook login activity . this is launcher activity.

public class LoginActivity extends AbsRuntimePermission {
private static final int REQUEST_Permission =10;


private TextView info;
private LoginButton loginButton;
private CallbackManager callbackManager;
private SparseIntArray mErrorString;
private Button facebook_button;
ProgressDialog progress;
private String facebook_id,f_name,m_name,l_name,gender,profile_image,full_name,email_id;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//for permission in manifest file cause it is launch activity

requestAppPermissions(new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.CALL_PHONE,
Manifest.permission.INTERNET},R.string.msg,REQUEST_Permission);

//facebook
info = (TextView) findViewById(R.id.info);
loginButton = (LoginButton) findViewById(R.id.login_button);
progress = new ProgressDialog(LoginActivity.this);
progress.setMessage(getResources().getString(R.string.please_wait_facebooklogin));
progress.setIndeterminate(false);
progress.setCancelable(false);

facebook_id=f_name=m_name=l_name=gender=profile_image=full_name=email_id="";

//facebook sdk
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();

//register callback object for facebook result

LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
progress.show();
Profile profile = Profile.getCurrentProfile();
if(profile != null){
facebook_id = profile.getId();
f_name=profile.getFirstName();
m_name=profile.getMiddleName();
l_name=profile.getLastName();
profile_image=profile.getProfilePictureUri(400,400).toString();
}
//show Toast
GraphRequest request =GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback(){
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
email_id = object.getString("email");
gender = object.getString("gender");
String Profile_name = object.getString("name");
long fb_id = object.getLong("id"); //use this for logout

//Starting a new activity using this information
Intent i = new Intent(LoginActivity.this, MainActivity.class);
i.putExtra("type", "facebook");
i.putExtra("Facebook_id", facebook_id);
i.putExtra("f_name", f_name);
i.putExtra("m_name", m_name);
i.putExtra("l_name", l_name);
i.putExtra("full_name", full_name);
i.putExtra("Profile_image", profile_image);
i.putExtra("email_id", email_id);
i.putExtra("gender", gender);

progress.dismiss();
startActivity(i);
finish();

}catch (JSONException e){
e.printStackTrace();
}
}
});
request.executeAsync();
}

@Override
public void onCancel() {
Toast.makeText(LoginActivity.this,getResources().getString(R.string.login_canceled_facebooklogin),Toast.LENGTH_SHORT).show();
progress.dismiss();
}

@Override

public void onError(FacebookException error) {
Toast.makeText(LoginActivity.this,getResources().getString(R.string.login_failed_facebooklogin),Toast.LENGTH_SHORT).show();
progress.dismiss();
}
});

//facebook button click
facebook_button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
LoginManager.getInstance().logInWithReadPermissions(LoginActivity.this, Arrays.asList("public_profile","user_friends","email"));

}
});
}

private void logoutFromFacebook(){
try{
if(AccessToken.getCurrentAccessToken()==null){
return;
}

}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode,resultCode,data);
}

@Override
public void onPermissionGranted(int requestCode) {

//anything after permission Granted
Toast.makeText(getApplicationContext(),"Permission granted",Toast.LENGTH_LONG).show();

}



public void goTo(View view) {
Intent intent = new Intent(this, MainActivity.class);
this.startActivity(intent);
}


}

Answer

Android SharedPreferences allows us to store private primitive application data in the form of key-value pair.For More details follow this.

To read from shared preference use something like this.

String dateFromSharedPreferences=getActivity().getPreferences(Context.MODE_PRIVATE);
sharedPref.getString("Username");

To save into shared preference

    SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString("Username", YourUsername);
    editor.putString("Password", YourPassword);
    editor.apply();

You are passing the values from LoginActivity to MainActivity so code in MainActivity at onCreate function like following to get the passed values ans show in textview.

((TextView)findViewById(R.id.tv1)).setText(getIntent().getExtras().getString("full_name"));
((TextView)findViewById(R.id.tv2)).setText(getIntent().getExtras().getString("email_id"));
((TextView)findViewById(R.id.tv3)).setText(getIntent().getExtras().getString("gender"));