Scene introduction
<>
Lightweight preference database is lightweight storage , It is mainly used to save some common configuration of the application , It is not suitable for the scenario of storing large amount of data and changing data frequently . The user's data is saved in a file , It can be stored permanently on the device . It should be noted that the instance accessed by the user contains all the data of the file , And is always loaded in the memory of the device , And through the lightweight preference database API Complete data operation .
Interface description
<>
Lightweight preference database provides local application with API, Support local application to read and write a small amount of data and observe data changes . The data storage form is key value pair , The type of the key is string , The storage data types of values include integers , String type , Boolean type , float , Long integer , String type Set aggregate .
Create database
Through the auxiliary class of database operation, you can get the Preferences example , For database operations .
surface 1 Lightweight preference database creation interface
Class name
Interface name
describe
DatabaseHelper
Preferences getPreferences(String name)
Get the corresponding file Preferences Single instance , For data manipulation .
Query data
By calling Get Series approach , Different types of data can be queried .
surface 2 Lightweight preference database query interface
Class name
Interface name
describe
Preferences
int getInt(String key, int defValue)
Gets the corresponding int Value of type .
Preferences
float getFloat(String key, float defValue)
Gets the corresponding float Value of type .
insert data
adopt Put The method of series can be modified Preferences Data in the instance , adopt flush perhaps flushSync take Preferences Instance persistence .
surface 3 Lightweight preference database insertion interface
Class name
Interface name
describe
Preferences
Preferences putInt(String key, int value)
set up Preferences The corresponding key in the instance int Value of type .
Preferences
Preferences putString(String key, String value)
set up Preferences The corresponding key in the instance String Value of type .
Preferences
void flush()
take Preferences Instance writes file asynchronously .
Preferences
boolean flushSync()
take Preferences Instance synchronous write file .
Observe the change of data
The lightweight preference database also provides a series of interface change callback , Used to observe changes in data . Developers can rewrite onChange Method to define the behavior of the observer .
surface 4 Lightweight preference database interface change callback
Class name
Interface name
describe
Preferences
void registerObserver(PreferencesObserver preferencesObserver)
Registered observer , Used to observe data changes .
Preferences
void unRegisterObserver(PreferencesObserver preferencesObserver)
Cancel observer .
Preferences.PreferencesObserver
void onChange(Preferences preferences, String key)
The callback method of observer , Any data change will call back the method .
Delete data file
By calling the following two interfaces , Data files can be deleted .
surface 5 Lightweight preference database deletion interface
Class name
Interface name
describe
DatabaseHelper
boolean deletePreferences(String name)
Delete file and file corresponding Preferences Single instance .
DatabaseHelper
void removePreferencesFromCache(String name)
Delete the Preferences Single instance .
Moving database files
surface 6 Lightweight preference database mobile interface
Class name
Interface name
describe
DatabaseHelper
boolean movePreferences(Context sourceContext, String sourceName, String
targetName)
Moving database files .
Development steps
<>
1. preparation , Import to lightweight preference database SDK To development environment .
2. obtain Preferences example .
Read the specified file , Load data into Preferences example , For data manipulation .
DatabaseHelper databaseHelper = new DatabaseHelper(context); //
context The input parameter type is ohos.app.Context. String fileName = "name"; //
fileName Represents the file name , Its value cannot be empty , It cannot contain a path , The default storage directory can be accessed through the context.getPreferencesDir() obtain .
Preferences preferences = databaseHelper.getPreferences(fileName);
3. Read data from the specified file .
First, get the Preferences example , And then with the help of Preferences API Read data .
java Interface Read integer data
int value = preferences.getInt("intKey", 0);
4. Writes data to the specified file .
First, get the Preferences example , And then with the help of Preferences
API Write data to Preferences example , adopt flush perhaps flushSync take Preferences Instance persistence .
asynchronous :
preferences.putInt("intKey", 3); preferences.putString("StringKey", "String
value"); preferences.flush();
synchronization :
preferences.putInt("intKey", 3); preferences.putString("StringKey", "String
value"); preferences.flushSync();
5. Registered observer .
Developers can contact Preferences Instance registration observer , The observer object needs to be implemented Preferences.PreferencesObserver Interface .flushSync() or flush() After implementation , The Preferences Of all observers registered by the instance onChange() Methods are called back . Please log off when you no longer need an observer .
private class PreferencesChangeCounter implements
Preferences.PreferencesObserver { final AtomicInteger notifyTimes = new
AtomicInteger(0); @Override public void onChange(Preferences preferences,
String key) { if ("intKey".equals(key)) { notifyTimes.incrementAndGet(); } } }
// towards preferences Instance registration observer PreferencesChangeCounter counter = new
PreferencesChangeCounter(); preferences.registerObserver(counter); //
Modify data preferences.putInt("intKey", 3); boolean result = preferences.flushSync();
// After data modification ,onChange Method is called back ,notifyTimes == 1 int notifyTimes =
counter.notifyTimes.intValue(); // towards preferences Instance logoff observer
preferences.unRegisterObserver(counter);
6. remove Preferences example .
Remove the file from memory Preferences Single instance . remove Preferences Single instance time , The application is not allowed to use this instance for data operation , Otherwise, there will be data consistency problems .
DatabaseHelper databaseHelper = new DatabaseHelper(context); String fileName =
"name"; // fileName Represents the file name , Its value cannot be empty , It cannot contain a path .
databaseHelper.removePreferencesFromCache(fileName);
7. Delete the specified file .
Remove the file from memory Preferences Single instance , And delete the specified file and its backup file , Corrupt file . When deleting the specified file , The application is not allowed to use this instance for data operation , Otherwise, there will be data consistency problems .
DatabaseHelper databaseHelper = new DatabaseHelper(context); String fileName =
"name"; // fileName Represents the file name , Its value cannot be empty , It cannot contain a path . boolean result =
databaseHelper.deletePreferences(fileName);
8. Move specified file .
Move files from source path to destination path . When moving files , The application is not allowed to operate the file data any more , Otherwise, there will be data consistency problems .
Context targetContext = XXX; DatabaseHelper databaseHelper = new
DatabaseHelper(targetContext); String srcFile = "srcFile"; //
srcFile Represents the name of the source file or the absolute path of the source file , Cannot be a relative path , Its value cannot be empty . When srcFile When passing in only the file name ,srcContext Cannot be empty . String
targetFile = "targetFile"; // targetFile Represents the target file name , Its value cannot be empty , It cannot contain a path . Context
srcContext = XXX; boolean result =
databaseHelper.movePreferences(srcContext,srcFile,targetFile);
Technology
Daily Recommendation