how to use getSharedPreferences in android
Asked Answered
G

4

85

I have an application in which I have to implement a "Login" activity. I have these components:

  1. EditText username
  2. EditText password
  3. Button Login
  4. Button Cancel

I want that my application to remember the login details of the user once the user has logged in until the user would press the "log out" button. I'm not using preferences in my xml.

How do I get the getSharedPreferences(String name, int mode) to work in my application?

Goldwin answered 10/5, 2011 at 12:31 Comment(0)
E
166

First get the instance of SharedPreferences using

SharedPreferences userDetails = context.getSharedPreferences("userdetails", MODE_PRIVATE);

Now to save the values in the SharedPreferences

Editor edit = userDetails.edit();
edit.putString("username", username.getText().toString().trim());
edit.putString("password", password.getText().toString().trim());
edit.apply();

Above lines will write username and password to preference

Now to to retrieve saved values from preference, you can follow below lines of code

String userName = userDetails.getString("username", "");
String password = userDetails.getString("password", "");

(NOTE: SAVING PASSWORD IN THE APP IS NOT RECOMMENDED. YOU SHOULD EITHER ENCRYPT THE PASSWORD BEFORE SAVING OR SKIP THE SAVING THE PASSWORD)

Exorbitance answered 10/5, 2011 at 12:36 Comment(3)
umm.. i have another question.. how can i restore the preferences?Goldwin
caveat -- don't store passwords for sensitive things like this unless they're encrypted.Lamina
@eqbridges. Spot on! Preferences are stored in a xml file on the device. The API simply writes and reads from the file. Be sure not to store passwords.Guernsey
S
19
//Set Preference
SharedPreferences myPrefs = getSharedPreferences("myPrefs", MODE_PRIVATE);
SharedPreferences.Editor prefsEditor;  
prefsEditor = myPrefs.edit();  
//strVersionName->Any value to be stored  
prefsEditor.putString("STOREDVALUE", strVersionName);  
prefsEditor.commit();

//Get Preferenece  
SharedPreferences myPrefs;    
myPrefs = getSharedPreferences("myPrefs", MODE_PRIVATE);  
String StoredValue=myPrefs.getString("STOREDVALUE", "");

Try this..

Sunda answered 13/2, 2012 at 4:40 Comment(2)
MODE_WORLD_READABLE is not the right choice for storing sensitive data, and it also become deprecated in API level 17.Skew
MODE_WORLD_READABLE is currently deprecated!Minne
U
3

If someone used this:

val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)

PreferenceManager is now depricated, refactor to this:

val sharedPreferences = context.getSharedPreferences(context.packageName + "_preferences", Context.MODE_PRIVATE)
Ulphi answered 11/5, 2020 at 15:5 Comment(1)
getDefaultSharedPreferences() is still working for SettingsActivity.java and linked root_preferences.xml files where users set their preferences.Unbar
T
2

After reading around alot, only this worked: In class to set Shared preferences:

SharedPreferences userDetails = getApplicationContext().getSharedPreferences("test", MODE_PRIVATE);
SharedPreferences.Editor edit = userDetails.edit();
edit.clear();
edit.putString("test1", "1");
edit.putString("test2", "2");
edit.commit();

In AlarmReciever:

SharedPreferences userDetails = context.getSharedPreferences("test", Context.MODE_PRIVATE);
String test1 = userDetails.getString("test1", "");
String test2 = userDetails.getString("test2", "");
Tuyettv answered 12/11, 2015 at 20:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.