[Android Official Document Reading Notes] saves data and android Reading Notes
Android provides three Data Storage Methods: SharePreference, files, and databases.
1. SharePreference
To save a relatively small key-values set, you can use the SharedPreferences API. SharedPreferences object to point to the file containing the key-value Pair and provide a simple read/write method. Each SharedPreferences file is managed by the framework and can be used privately or shared. It is essentially an xml file.
Data Reading:
SharedPreferences sharedPref = getActivity (). getPreferences (Context. MODE_PRIVATE );
Long highScore = sharedPref. getInt (key, defaultValue );
Data Writing:
SharedPreferences sharedPref = getActivity (). getPreferences (Context. MODE_PRIVATE );
SharedPreferences. Editor editor = sharedPref. edit ();
Editor. putInt (getString (R. string. saved_high_score), newHighScore );
Editor. commit ();
2. File
File objects are suitable for reading or writing large amounts of data at start-to-finish. File objects are suitable for reading or writing large amounts of data, but the disadvantage is that it is difficult to update data. Android file storage is divided into internal storage and external storage. Internal Storage features: always available. Saved files can only be used for default applications. When you detach an application, the system deletes all files from the internal storage. Therefore, to ensure that files can be accessed by both users and other applications, internal storage is undoubtedly the best choice. External storage features: it is not always available; it is globally readable, so stored files may be read by people out of control; users want to uninstall applications, the application file will be deleted only when the application file is saved in the getExternalFilesDir () directory. Therefore, external storage is undoubtedly the best way to share files that do not require access and files that are shared with other applications or that allow users to access files using computers.
When saving files in internal storage, you can call either of the following two methods to obtain the corresponding directory file:
GetFilesDir () indicates the file in the internal directory of the application.
GetCacheDir () indicates the files in the internal directory of the files temporarily cached by the application.
External Store permissions:
<Manifest...>
<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>
...
</Manifest>
3. Database
Android uses SQLite to support database operations. SQLite is a lightweight database that supports basic SQL syntax and is a common data storage method. Android provides a class named SQLiteDatabase for this database, which encapsulates APIs for database operations. Generally, duplicate or structured data is stored in databases.
Data Reading example:
Cursor c = db. query (
FeedEntry. TABLE_NAME, // The table to query
Projection, // The columns to return
Selection, // The columns for the WHERE clause
SelectionArgs, // The values for the WHERE clause
Null, // don't group the rows
Null, // don't filter by row groups
SortOrder // The sort order
);
Example of updating a data write database:
SQLiteDatabase db = mDbHelper. getReadableDatabase ();
ContentValues values = new ContentValues (); // New value for one column
Values. put (FeedEntry. COLUMN_NAME_TITLE, title); // Which row to update, based on the ID
String selection = FeedEntry. COLUMN_NAME_ENTRY_ID + "LIKE? ";
String [] selectionArgs = {String. valueOf (rowId )};
Int count = db. update (FeedReaderDbHelper. FeedEntry. TABLE_NAME, values, selection, selectionArgs );
Analysis of several common methods for Android to save data
It can be applied to mobile phones to help us achieve many needs. For example, saving data on Android is an important operation skill. Summary of concepts related to Android components: Android value passing method: Details: Android horizontal and vertical screen switching: Correct implementation method: Sharing Android Development Environment Configuration Overview: Android NDK: Specific roles: A Comparison of most of the software we are familiar typical features, use existing data to obtain results based on different requirements. For example, office software such as Officeword, Excel, and PowerPoint are commonly used to help us fulfill certain requirements, at the same time, the data or documents generated by the software can be read and further optimized by other software. At this level, it can be seen that the software shares data through the same file standards. However, the biggest difference for Android is that the data or files stored by the application software on its platform are private and can only access the data resources it contains through itself. So based on such restrictions, how can we achieve data sharing between different programs on the Android platform? The answer is very simple-the application ContentProviders is built on the Android platform to define unified data standards. Android provides ContentProviders for different data types to meet various needs. For example, Image, Audio, Video, and address book information. Before reading the following documents, you 'd better familiarize yourself with the concept of Content Providers. With the Content Providers mentioned above, the next step is to process the storage link in the shared file process. Here there are four methods that are applicable to different situations. They both have their respective advantages and disadvantages. Therefore, before deciding which method to use, developers should consider whether the current operation is suitable for the selected method. Preferences Files Databases Network The following describes the four Android Data Storage Methods for appeal: Preferences analyzes the structure of the stored data, which is a relatively lightweight data storage method. Similar to our frequently-used INI file storage software initialization settings, it is also commonly used in the Android platform for storing simple parameter settings. For example, you can use it to save the last user modification or custom parameter settings, and keep the original settings after the program is started again. The Context. getSharedPreferences () method is used to read and write values. This method sets the name to allow other modules in the same program to share data. If you do not need to share data with other modules, you can use Activity. getPreferences () to keep the data private. It is important to emphasize that Preferences data cannot be shared directly between multiple programs (excluding Content Providers ). Use an instance to understand the actual usage: import android. app. activity; import android. content. sharedPreferences; public class Calc extends Activity {public static final String PREFS_NAME = "MyPrefsFile ";.... override protected void onCreate (Bundle state) {super. onCreate (state );.... // Restore preferences SharedPreferences settings = getSharedPreferences (PREFS_NAME, 0); boolean silent = settings. getBoolean (...... remaining full text>
How does android program save data?
SQL is already very simple. If you want to learn programming, the database is necessary. Make up for it. Don't be too hasty. I/O operations can also be used to replace it.