Android – SharedPreferences storage
“This is the fourth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
Different from file storage, SharedPreferences uses key-value pairs to store data. When you save a data, you need to provide a key for the data so that the corresponding value can be read by using this key. SharedPreferences also supports different data types. If the storage type is integer, the data type is integer, and if the storage type is string, the data type is string.
1. Store the data in SharedPreferences
To store data using SharedPreferences, you must first obtain the SharedPreferences object. Android provides three methods for obtaining the SharedPreferences object.
The getSharedPreferences () method in the Context class:
This method takes two parameters. One is to specify the name of the SharedPreferences file, or to create one if the specified file does not exist. The default location of the SharedPreferences file is /data/data//shared_prefs/. The second parameter specifies the mode of operation. Currently, only MODE_PRIVATE is available. It has the same effect as passing 0, indicating that only the current application can use the SharedPreferences file.
The getPreferences () method in the Activity class:
This method is similar to the getPreferences () method in the Context, except that the method in the Activity only accepts an action mode parameter because the current Activity class name is used as the file name for SharedPreferences.
PreferencesManager getDefaultSharePreferences in class () method:
This is a static method that takes a Context parameter and automatically prefixes the SharedPreferences file with the package name of the current application. After obtaining the SharedPreferences object, it begins to store data in the SharedPreferences file, which can be divided into three implementation steps:
(1) Call the Edit () method of the SharedPreferences object to get a SharedPreferences.
(2) Add data to the SharedPreferences. Editor object. For example, adding a Boolean is like adding a normal Boolean () method.
(3) Call the apply () method to commit the added data, thus completing the data storage operation.
Here is a code that shows how to store data:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button saveData = (Button) findViewById(R.id.saveData);
saveData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();
editor.putString("name"."xiaolaohu");
editor.putInt("age".18);
editor.putBoolean("merried".false); }}); }Copy the code
2. Read data from SharedPreferences
It’s easier to read the data from the SharedPreferences file. The SharedPreferences object provides a set of GET methods for reading stored data. Each GET method corresponds to a PUT method in SharedPreferences. For example, to read a Boolean, use the getBoolean () method. These GET methods typically take two arguments. The first argument is the key that is passed in for storing the data, and the second is the default value that is returned when the passed key does not find the value.
The following code shows how to read data:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button restoreData = (Button) findViewById(R.id.restoreData);
restoreData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {SharedPreferences pref = getSharedPreferences ()"data",MODE_PRIVATE);
String name = pref.getString("name"."");
int age = pref.getInt("age".0);
boolean married = pref.getBoolean("married".false);
Log.d("TAG"."name is "+name);
Log.d("TAG"."age is "+age);
Log.d("TAG"."married is "+married); }}); }Copy the code
As you can see, in the click event, we first get the SharedPreferences object using the getSharedPreferences () method, and then call the getString (), getInt (), and getBoolean () methods to get the corresponding values. If no corresponding value is found, the default value passed in the method is used instead.