[Reprinted] Four Data Storage Methods on the Android platform

Source: Internet
Author: User
Tags java fileinputstream
Most of what we are familiar Software They all have a typical feature, and the existing data can be used to obtain the corresponding results based on different needs. 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 passes the same File Standard to share data. However, Android The biggest difference is that the data or files stored by the application software on the platform are private and can only access the data resources it contains through itself.

Based on such restrictions, how can we achieve different implementations on the Android platform?ProgramWhat about data sharing between them? 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 own advantages and disadvantages, so whenDevelopmentBefore deciding which method to apply, consider whether the current operation is suitable for the selected method.

    • Preferences
    • Files
    • Databases
    • Network

Next we will introduce four appeal methods in sequence:
Preferences analyzes the structure of stored data, which is a relatively lightweight Method for storing data. Similar to our common INI file storage software initializationSetIn the same way, the Android platform is often used to store 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:
01. Import android.App. Activity;
02. Import Android. content. sharedpreferences;
03.

04. Public class calc extends activity {
05. Public static final string prefs_name = "myprefsfile ";
06 ....
07.

08. @ override
09. Protected void oncreate (bundle state ){
10. Super. oncreate (State );
11.

12 ....
13.

14. // restore preferences
15. sharedpreferences settings = getsharedpreferences (prefs_name, 0 );
16. boolean silent = settings. getboolean ("silentmode", false );
17. setsilent (silent );
18 .}
19.

20. @ override
21. Protected void onstop (){
22. Super. onstop ();
23.

24. // save user preferences. We need an editor object
25. // make changes. All objects are from Android. Context. Context
26. sharedpreferences settings = getsharedpreferences (prefs_name, 0 );
27. sharedpreferences. Editor editor = settings. Edit ();
28. Editor. putboolean ("silentmode", msilentmode );
29.

30. // don't forget to commit your edits !!!
31. Editor. Commit ();
32 .}
33 .}

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: the same path and file name cannot be applied in other programs.CompositionParts)
In addition, before compiling the program, create a static file in RES/raw/tempfile. In this way, you can use resources. 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.
Sqlitedatabase is powerful and convenientFunctionIt provides 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. For more information, see examine databases (sqlite3.
The address saved by the database on the device is/data/package_name/databases.

NetworkNetworkThis method requires the device to maintain the network connection, so there are some restrictions. Two classes for related operations are listed below:

    • Java.net .*
    • Android.net .*

the preceding four methods are commonly used to store and read data on the Android platform.
View official documentation

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.