title: Store Access Token in Android Local Storage?
body: <p>I am not able to Store access-token,username  in the SharedPreferences.How can this be Done?</p>
  
  <p>Login Class</p>
  
  <pre><code>   public class Login extends AppCompatActivity implements View.OnClickListener {
  
  
  EditText userName, Password;
  Button login;
  public static final String LOGIN_URL = "http://192.168.100.5:84/Token";
  public static final String KEY_USERNAME = "UserName";
  public static final String KEY_PASSWORD = "Password";
  String username, password, accesstoken, tokentype;
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_login);
      userName = (EditText) findViewById(R.id.login_name);
      Password = (EditText) findViewById(R.id.login_password);
      userName.setHint(Html.fromHtml("&lt;font color='#008b8b' style='italic'&gt;Username&lt;/font&gt;"));
      Password.setHint(Html.fromHtml("&lt;font color='#008b8b'&gt;Password&lt;/font&gt;"));
      login = (Button) findViewById(R.id.login);
      login.setOnClickListener(this);
  }
  
  private void UserLogin() {
  
      username = userName.getText().toString().trim();
      password = Password.getText().toString().trim();
  
  
      StringRequest stringRequest = new StringRequest(Request.Method.POST, LOGIN_URL,
              new Response.Listener&lt;String&gt;() {
                  @Override
                  public void onResponse(String response) {
                      try {
                          JSONObject jsonObject = new JSONObject(response);
                          accesstoken = jsonObject.getString("access_token");
                          tokentype = jsonObject.getString("token_type");
                          SessionManagement session = new SessionManagement(Login.this);
                          session.createLoginSession(accesstoken);
                          openProfile();
  
                      } catch (JSONException e) {
                          e.printStackTrace();
                      }
  
                  }
              },
              new Response.ErrorListener() {
                  @Override
                  public void onErrorResponse(VolleyError error) {
                      Toast.makeText(Login.this, error.toString(), Toast.LENGTH_LONG).show();
                  }
              }) {
  
  
          @Override
          public Map&lt;String, String&gt; getHeaders() throws AuthFailureError {
              Map&lt;String, String&gt; params = new HashMap&lt;String, String&gt;();
              //  params.put("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
              return params;
          }
  
  
          @Override
          protected Map&lt;String, String&gt; getParams() {
              Map&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
              map.put(KEY_USERNAME, username);
              map.put(KEY_PASSWORD, password);
              map.put("grant_type", "password");
              return map;
          }
      };
  
  
      stringRequest.setRetryPolicy(new DefaultRetryPolicy(
              60000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
  
  
      RequestQueue requestQueue = Volley.newRequestQueue(this);
      requestQueue.add(stringRequest);
  }
  
  
  private void openProfile() {
      Intent intent = new Intent(this, Home.class);
      intent.putExtra(KEY_USERNAME, username);
      startActivity(intent);
  }
  
  @Override
  public void onClick(View v) {
  
  
      UserLogin();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>SessionManagement class</p>
  
  <pre><code>   public class SessionManagement {
      SharedPreferences pref;
      SharedPreferences.Editor editor;
      Context _context;
  
      // Shared pref mode
      int PRIVATE_MODE = 0;
  
      // Sharedpref file name
      private static final String PREF_NAME = "AndroidHivePref";
  
  
      private static final String IS_LOGIN = "IsLoggedIn";
  
  
      public static final String KEY_USERNAME = "UserName";
  
      public static final String KEY_access_token = "access_token";
      public static final String KEY_TOKEN_TYPE = "token_type";
      public static final String KEY_MASTER_ID = "MasterID";
      public static final String KEY_NAME = "Name";
      public static final String KEY_Access = "Name";
  
  
      // Constructor
      public SessionManagement(Context context) {
          this._context = context;
          pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
          editor = pref.edit();
      }
  
      /**
       * Create login session
       */
      public void 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
  
  
          String user_name_new = pref.getString(KEY_USERNAME, null);
  
          Log.d("TAG", "Pass user name :" + username + " user_name_new:" + user_name_new);
          editor.commit();
  
      }
  
      /**
       * Check login method wil check user login status
       * If false it will redirect user to login page
       * Else won't do anything
       */
      public void checkLogin() {
          // Check login status
          if (!this.isLoggedIn()) {
              // user is not logged in redirect him to Login Activity
              Intent i = new Intent(_context, Login.class);
              // Closing all the Activities
              i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  
              // Add new Flag to start new Activity
              i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  
              // Staring Login Activity
              _context.startActivity(i);
          }
  
      }
  
  
      /**
       * Get stored session data
       */
      public HashMap&lt;String, String&gt; getUserDetails() {
          HashMap&lt;String, String&gt; user = new HashMap&lt;String, String&gt;();
          // user name
          user.put(KEY_USERNAME, pref.getString(KEY_USERNAME, null));
          user.put(KEY_access_token, pref.getString(KEY_access_token, null));
  
          user.put(KEY_TOKEN_TYPE, pref.getString(KEY_TOKEN_TYPE, null));
          user.put(KEY_MASTER_ID, pref.getString(KEY_MASTER_ID, null));
          user.put(KEY_access_token, pref.getString(KEY_access_token, null));
          user.put(KEY_NAME, pref.getString(KEY_NAME, null));
          user.put(KEY_Access, pref.getString(KEY_Access, null));
  
  
          // return user
          return user;
      }
  
      /**
       * Clear session details
       */
      public void logoutUser() {
  
          editor.clear();
          editor.commit();
  
          // After logout redirect user to Loing Activity
          Intent i = new Intent(_context, Login.class);
          // Closing all the Activities
          i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  
          // Add new Flag to start new Activity
          i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  
          // Staring Login Activity
          _context.startActivity(i);
      }
  
      /**
       * Quick check for login
       **/
      // Get Login State
      public boolean isLoggedIn() {
          return pref.getBoolean(IS_LOGIN, false);
      }
  
  }
  </code></pre>
  
  <p>How the SessionManagement Class be used in the LOgin CLass to store information Locally??</p>