Android SharedPreferences enables data storage

In addition to SQLite database, SharedPreferences is also a lightweight data storage method. Different from the file storage method, SharedPreferences uses key value data to store data. Moreover, SharedPreferences also supports a variety of different data types. Therefore, using SharedPreferences for data persistence is much more convenient than using files. Let's take a look at its specific usage.

How to store data in SharedPreferences

To use SharedPreferences to store data, you first need to get the SharedPreferences object. Android mainly provides three methods to get SharedPreferences objects.

1. Getsharedpreferences() method in context class

This method receives two parameters. The first parameter is used to specify the name of the SharedPreferences file. If the specified file does not exist, one will be created. The SharedPreferences files are stored in / data / data / / shared_ In the prefs / directory. The second parameter is used to specify the operation mode. There are mainly two modes to select, mode_ Private and mode_ MULTI_ PROCESS。 MODE_ Private is still the default operation mode. The effect is the same as that of directly passing in 0, which means that only the current application can read and write this SharedPreferences file. MODE_ MULTI_ Process is generally used to read and write the same SharedPreferences file in multiple processes. Similarly, mode_ WORLD_ Readable and mode_ WORLD_ Both writeable modes have been abandoned in Android version 4.2.

2. Getpreferences () method in activity class

This method is very similar to the getsharedpreferences () method in context, but it only receives an operation mode parameter, because when using this method, the currently active class name will be automatically used as the file name of SharedPreferences.

3. Getdefaultsharedpreferences() method in preferencemanager class

This is a static method that receives a context parameter and automatically names the SharedPreferences file with the package name of the current application as a prefix.

After you get the SharedPreferences object, you can start storing data in the SharedPreferences file, which can be implemented in three steps.

1. Call the edit () method of the SharedPreferences object to get a sharedpreferences.editor object. 2. Add data to the sharedpreferences.editor object. For example, putboolean method is used to add a Boolean data, putstring () method is used to add a string, and so on. 3. Call the commit () method to submit the added data to complete the data storage operation.

We have unconsciously introduced a lot of theoretical knowledge, so let's quickly experience the usage of shared preferences storage through an example.

Create a new sharedpreferencestest project and modify the activity_ The code in main.xml is as follows:

Here we simply put a button to store some data in the shared preferences file. Then modify the code in mainactivity as follows:

You can see that here, a click event is registered for the button, and then in the click event, the file name of SharedPreferences is specified as data through the getsharedpreferences() method, and the sharedpreferences.editor object is obtained. Then we add three different types of data to this object, and finally call the commit () method to commit, thus completing the operation of data storage.

Next, we will naturally take a look at how to read these data from the shared preferences file.

Read data from SharedPreferences

A series of get methods are provided in the SharedPreferences object to read the stored data. Each get method corresponds to a put method in the SharedPreferences. Editor. For example, the getboolean () method is used to read a Boolean data, and the getString () method is used to read a character string. These get methods receive two parameters. The first parameter is the key, and the corresponding value can be obtained by passing in the key used to store data. The second parameter is the default value, which indicates what default value will be returned when the passed in key cannot find the corresponding value.

Let's experience it through examples. We still continue to develop and modify the activity based on the shared preferences test project_ The code in main.xml is as follows:

A button to restore data is added here. We hope to read data from the shared preferences file by clicking this button.

Modify the code in mainactivity as follows:

It can be seen that in the click event of the restore data button, we first get the SharedPreferences object through the getsharedpreferences() method, and then call its getstring(), getint() and getboolean() methods to obtain the name, age and married status stored above. If the corresponding value is not found, the default value passed in the method will be used instead, Finally, print these values through log.

Through this simple example, we learned how to use shared preferences to realize data storage. In contrast, SharedPreferences storage is indeed much simpler and more convenient than text storage, and there are many application scenarios. For example, SharedPreferences technology is actually used in the preference setting function in many applications.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>