Analysis of several common methods for Android to save data

Source: Internet
Author: User
Tags java fileinputstream

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.

 
 
  1. Preferences  
  2. Files  
  3. Databases  
  4. 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:

 
 
  1. import android.app.Activity;  
  2. import android.content.SharedPreferences;  
  3. public class Calc extends Activity {  
  4. public static final String PREFS_NAME = "MyPrefsFile";  
  5. . . . .   
  6. Override  
  7. protected void onCreate(Bundle state){  
  8. super.onCreate(state);  
  9. . . . .  
  10. // Restore preferences  
  11. SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);  
  12. boolean silent = settings.getBoolean("silentMode", false);  
  13. setSilent(silent);  
  14. }  
  15. @Override  
  16. protected void onStop(){  
  17. super.onStop();  
  18. // Save user preferences. We need an Editor object to  
  19. // make changes. All objects are from android.context.Context  
  20. SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);  
  21. SharedPreferences.Editor editor = settings.edit();  
  22. editor.putBoolean("silentMode", mSilentMode);  
  23. // Don't forget to commit your edits!!!  
  24. editor.commit();  
  25. }  

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:

 
 
  1. java.net.*  
  2. android.net.* 

The preceding four Android data storage methods are commonly used to store and read data on the Android platform.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.