Skip to content Skip to sidebar Skip to footer

Store Access Token In Android Local Storage?

I am not able to Store access-token,username in the SharedPreferences.How can this be Done? Login Class public class Login extends AppCompatActivity implements View.OnClickList

Solution 1:

Try this,

if (response.trim().equals("success")) {

        //add these 2 lines
        SessionManagement session=new SessionManagement(Login.this);
        session.createLoginSession(username, accesstoken,tokentype, masterid,name, access);

        openProfile();
    } else {
        Toast.makeText(Login.this, response, Toast.LENGTH_LONG).show();
    }

Edit your createLoginSession function:

/**
 * Create login session
 * */publicvoid createLoginSession(String username, String accesstoken,String tokentype, String masterid,String name, Integer access){
    // Storing login value as TRUE


    editor.putBoolean(IS_LOGIN, true);
    editor.putString(KEY_USERNAME, username);
    // Storing name in pref
    editor.putString(KEY_access_token, accesstoken);

    // Storing email in pref
    editor.putString(KEY_TOKEN_TYPE, tokentype);

    editor.putString(KEY_MASTER_ID, masterid);
    editor.putString(KEY_TOKEN_TYPE, tokentype);
    editor.putString(KEY_NAME, name);
    editor.putInt(KEY_Access, access);




    // commit changes
    editor.commit();


    String user_name_new=pref.getString(KEY_USERNAME, null)

    Log.d("TAG","Pass user name :"+username+" user_name_new:"+user_name_new);
}

Solution 2:

You can use shared preference to hold the login details/access token. please go through the code snippet.

publicclassPreferences {
publicstaticvoidsetAccessToken(@NonNull Context context, String token) {
    SharedPreferences sharedPreferences = context.getSharedPreferences("MySharedPref", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString("ACCESSTOKEN", token);
    editor.apply();
}

publicstaticStringgetAccessToken(@NonNull Context context) {
    SharedPreferences sharedPreferences = context.getSharedPreferences("MySharedPref", Context.MODE_PRIVATE);
    return sharedPreferences.getString("ACCESSTOKEN", null);
}}

similarly, you can store the username and password to shared prefs and retrieve them as you required.

Solution 3:

Follow these steps to save any type of data in local storage will works perfectly, Because this is running code of my application.

  1. Firstly create a static instance of your application in your application class. And in this application class make static instance of your PreferenceManager class where all operations will done.
publicclassMyAppextendsApplication {

publicstaticMyApp myApp ;
publicstaticMyPreferenceManager myPreferenceManager;

@OverridepublicvoidonCreate() {
    super.onCreate();

    myApp = this;

  }

/*For creating the context of the Whole app.*/publicstaticMyAppgetInstance() {
    return myApp ;
}

/*This is for getting the instance of the MyPreferenceManager class using the context of MyApp App.*/publicstaticMyPreferenceManagergetPreferenceManager() {
    if (myPreferenceManager == null) {
        myPreferenceManager = newMyPreferenceManager(getInstance());
    }

    return myPreferenceManager;
}

}
  1. Now code of your MyPreferenceManager class :
publicclassMyPreferenceManager {

Context context;

SharedPreferences sharedPreferences;

SharedPreferences.Editor editor;

privatestatic final StringPREF_NAME = "com.example.App";

publicstatic final StringKEY_ID = "id";
publicstatic final StringKEY_ACCESS_TOKEN = "access_token";

publicMyPreferenceManager(Context context) {

    this.context = context;

    sharedPreferences = context.getSharedPreferences(PREF_NAME,   Context.MODE_PRIVATE);

    editor = sharedPreferences.edit();

    editor.apply();

}

publicvoidputString(String key, String value) {

    editor.putString(key, value);

    editor.apply();

}

publicStringgetString(String key) {

    return sharedPreferences.getString(key, null);

}

//Method to clear the login data of the application.publicvoidclearLoginData() {

    editor.remove(KEY_ID);
    editor.remove(KEY_ACCESS_TOKEN);
    editor.apply();

}

}
  1. Now from any where you can save data to local storage from this code below.
MyApp.getInstance().getPreferenceManager().putString(MyPreferenceManager.KEY_ID, "1");
MyApp.getInstance().getPreferenceManager().putString(MyPreferenceManager.KEY_ACCESS_TOKEN, "oihfodfdshfhfoifhoifh3393");
  1. Now from any where you can get data from local storage from this code below.
Stringid= MyApp.getInstance().getPreferenceManager().getString(MyPreferenceManager.KEY_ID);
StringaccessToken= MyApp.getInstance().getPreferenceManager().getString(MyPreferenceManager.KEY_ACCESS_TOKEN);

Thanks.

Post a Comment for "Store Access Token In Android Local Storage?"