The Android mobile operating system is an open-source operating system based on the Linux platform. It can be applied to mobile phones to help us achieve many needs. For example, saving data on Android is an important operation skill.
- Android component concepts
- Detailed explanation of Android value passing Method
- Share the correct implementation mode of Android horizontal and vertical screen Switching
- Android Development Environment Configuration Overview
- Android NDK
Most of the software we are familiar with has a typical feature. The existing data can be used to obtain relevant 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
Next, we will introduce the following four Android Data Storage Methods:
Preferences analyzes the structure of stored data, which is a relatively lightweight Method for storing data. 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. You need 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("silentMode", false);
- setSilent(silent);
- }
- @Override
- protected void onStop(){
- super.onStop();
- // Save user preferences. We need an Editor object to
- // make changes. All objects are from android.context.Context
- SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
- SharedPreferences.Editor editor = settings.edit();
- editor.putBoolean("silentMode", mSilentMode);
- // Don't forget to commit your edits!!!
- editor.commit();
- }
- }
Files is the second method. You can create a file to save data on the storage device of the device or an external storage device. By default, files cannot be shared among different programs.
Write File: Call the Context. openFileOutput () method to create a file based on the specified path and file name. This method returns a FileOutputStream object.
Read files: Call the Context. openFileInput () method to return a standard Java FileInputStream object through the specified path and file name.
Note: In other programs, the same path and file name cannot be applied to operate files)
Before compiling the program, create a static file in res/raw/tempFile. openRawResource (R. raw. myDataFile) method returns an InputStream object to directly read the file content.
Databases includes the SQLite databases interface in the Android API. The Databases created by each program are private. In other words, the applications cannot access each other's databases.
Create a SQLiteDatabase object in the program, which contains most of the methods for interacting with the database, such as reading data or managing the current data. You can use the create () method of SQLiteDatabase and its subClassSQLiteOpenHelper to create a new database.
For SQLitedatabase, its powerful and convenient functions provide powerful storage functions for Android. In particular, it stores some complex data structures. For example, Android creates a unique data type for the address book, it contains many subsets and covers most data types such as "First Name", "Last Name", "PhoneNumber", and "Photo.
Android can use Sqlite3 database tool to view the table content in a specified database, and directly run SQL commands to quickly and conveniently operate SQLite database.
The address saved by the database on the device is/data/package_name/databases.
The Network obtains and stores data resources through the Network. This method requires the device to maintain the Network connection status, so there are some restrictions. Two classes for related operations are listed below:
- java.net.*
- android.net.*
The preceding four Android data storage methods are commonly used to store and read data on the Android platform.