Simple data saving of key values in the application of Android system programming introduction series

In the process of communication between applications and users, a series of data will be generated and transmitted. Some of these data are cached data used only in applications, and some are persistent data used multiple times or for a long time in different locations. For cached data, it is usually accessed and used by defining local variables or global variables in the code, which is accompanied by the whole process of programming; Persistent data needs to be saved in the system hard disk in a specific file format and accessed using the framework method provided by the system. According to the complexity of persistent data, there are three ways: lightweight SharedPreferences, database sqliteopenhelper or its encapsulated room, and binary access file. This paper mainly introduces several different types of persistent data.

For lightweight key value pair data, you can use the relevant classes implemented by the android.content.sharedpreferences sharing option interface to persist.

The data saved in the form of SharedPreferences will be saved in the internal storage space of the current application in the form of key value pairs and XML files. The files in the internal storage space of this application are only allowed to be read and written by the application to which it belongs. After the application is uninstalled, or through the system desktop - Settings - Application Management - current application - application data - clear data series operation, its internal storage space will also be emptied. This ensures the data security of the internal storage space to a certain extent.

The sharedpreferencesimpl class has been defined in the Android SDK as the implementation class of the SharedPreferences interface.

For the instantiated object of the SharedPreferences interface, where the context object of the context environment can be accessed, it can be obtained by calling the getsharedpreferences (string name, int mode) method of the context object. Its corresponding XML format file will be read and written throughout the life cycle of the current application.

Or you can also call the Activity object's getPreferences (int mode) method in the Activity interface, and the corresponding XML files are read and write only in the current interface life cycle.

In the getsharedpreferences (string name, int mode) method, the parameter name specifies the file name of the XML format file that stores the current data, and its value can be defined by the developer. The parameter mode is the opening method of the file, and its value is usually the context.mode that allows only the current application to access the file_ PRIVATE=0; Before Android 6.0, that is, api23, this value can also be context.mode that allows multi process read-write synchronization_ MULTI_ Process = 4, however, various abnormal problems will occur in this mode, so this version will be discarded after; Before Android 4.2, that is, api17, the mode value can also be context.mode that allows other applications to read the file_ WORLD_ Readable = 1, and context.mode that allows other applications to write the file_ WORLD_ Writeable = 2, but neither of these two values can guarantee the data security of the current application, so this version will be discarded later.

In addition, if you don't like the SharedPreferences impl implementation class defined by the system, you can customize an implementation class of the SharedPreferences interface during development. You can use context.getsharedpreferences (string name, int mode) to obtain the location of the instantiated object and replace it with a self-defined implementation class object.

In short, when obtaining the SharedPreferences object, the system checks whether there is an XML format file with the specified name in the internal storage space of the current application. If not, it will be created first. After that, the system will open the file, and the file can be read and written through the SharedPreferences object.

If you want to write data to the file where the created SharedPreferences object is located, you only need to call the edit () method of the object to obtain the object of android.content.sharedpreferences.editor interface type.

Similar to the data transfer method in intent intention used in interface interaction, in the sharedpreferences.editor interface object, you can use putboolean (string key, Boolean value) to set boolean type data, putfloat (string key, float value) to set float type data, putstringset (string key, set < string > values) to set string set data, etc. In this series of setting methods, the parameter key is the only string type value in the SharedPreferences file to mark the current data; The second parameter, value, is the data value to be persisted.

In addition, in the sharedpreferences.editor interface object, you can also use the remove (string key) method to delete the data content marked by the existing parameter key. Or directly use the clear () method to clear all the data contents set.

After setting or deleting data through the SharedPreferences.Editor interface object, it calls its apply () or commit () method to submit all the data set up at once. After the system is submitted, the system will save the data set in the SharedPreferences file.

If you want to read the data of the file where the SharedPreferences object is located, there is no cumbersome step of writing the data to the file. You only need to directly call getboolean (string key, Boolean defvalue) of SharedPreferences object to obtain the data value of boolean type, getfloat (string key, float defvalue) to obtain the data value of float type, and getstringset (string key, set < string > defvalues) to obtain the data value of set < string > type. For this series of acquisition methods, the parameter key is consistent with the parameter key in the setting method when writing the file to mark the response data; Parameter 2 DefaultValue is the default data value. When the data corresponding to parameter key is not saved in the SharedPreferences file, the value set by DefaultValue will be returned.

If the amount of saved data is not large, you can also directly call the getall() method of the SharedPreferences object to obtain map < string,? > All data of collection type, and then operate and process the obtained data separately.

In addition, the contents (string key) method of the SharedPreferences object can only judge whether there is the data content marked by the parameter key in the file where the current SharedPreferences object is located, and return the boolean result.

You can use the registeronsharedpreferencechangelistener (SharedPreferences. Onsharedpreferencechangelistener listener) method of the SharedPreferences object to monitor the modification of the current SharedPreferences file in real time. The parameter listener is an instantiated object of the android.content.sharedpreferences.onsharedpreferencechangelistener interface, which implements the onsharedpreferencechanged (SharedPreferences, string key) method.

Once the listener is registered, after the key of the data mark in the file corresponding to the parameter SharedPreferences is modified, the onsharedpreferences changed (SharedPreferences, string key) method in the listener will be called back by the system.

For registered listener, remember to call the SharedPreferences object's unregisterOnSharedPreferencechangelistener (SharedPreferences.OnSharedPreferencechangelistener listener) after you do not need to monitor, and revoke the previously registered OnSharedPreferencechangelistener object to prevent subsequent memory leaks.

The lightweight shared preferences storage method can easily store some simple data, and its memory efficiency is relatively high. However, if all data in the application is stored in this way, the operation efficiency of shared preferences files will be reduced, and all data will be accessed in the form of key value pairs, which will also increase the amount of code. So is there any more suitable storage method for different types of data? For details, please pay attention to the next article.

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
分享
二维码
< <上一篇
下一篇>>