Four Methods for saving data in Android Development

Source: Internet
Author: User
In Android development, we will be exposed to four data storage methods, each of which is different. The following lists the features of different storage methods in Android development.
1. preferences
Preferences is a lightweight data storage method. The specific usage is as follows:
Save the value in:
Bytes ---------------------------------------------------------------------------------------------
Sharedpreferences. Editor uses data = getsharedpreferences ("data", 0). Edit ();
Using data. putstring ("name", "shenrenkui ");
Using data. Commit ();
Bytes ---------------------------------------------------------------------------------------------
Value in B:
Bytes ---------------------------------------------------------------------------------------------
Sharedpreferences upload data = getsharedpreferences ("data", 0 );
String data = response data. getstring ("name", null );
Log. I (TAG, "Data =" + data );
Bytes ---------------------------------------------------------------------------------------------
Note: context. the getsharedpreferences (string name, int type) parameter is the same as the data permission attribute when data is created. The process of storage and value is a bit like hashmap, but it is more user-friendly than hashmap, getxxx (Object key, object defualtreturnvalue). The second parameter is the value returned when the key you want does not exist. This saves a lot of logical judgment ....

Ii. Files

The file that is not found on Android is the purebred file in j2se, which has powerful functions. Here, it is even a passing style.
Bytes ---------------------------------------------------------------------------------------------
// Create a file
File = new file (file_path, file_name );
File. createnewfile ();

// Open the outputstream of the file
Out = new fileoutputstream (File );
String infotowrite = "the last word on the paper is superficial. I know this is a must ";
// Convert the string into a byte array and write it to the file
Out. Write (infotowrite. getbytes ());
// Close the outputstream of the file
Out. Close ();

// Open the inputstream of the file
In = new fileinputstream (File );
// Read all the file content into the byte array
Int length = (INT) file. Length ();
Byte [] temp = new byte [length];
In. Read (temp, 0, length );
// Encode the byte array with a UTF-8 and store it in the display string
Display = encodingutils. getstring (temp, text_encoding );
// Close the inputstream of the file
In. Close ();
} Catch (ioexception e ){
// Print error information to logcat
Log. E (TAG, E. tostring ());
This. Finish ();
}
// Read from resource
Inputstream is = getresources (). getrawresource (R. Raw. File name)
Bytes ---------------------------------------------------------------------------------------------
Iii. Databases

Android is embedded with the relational database sqlite3, which is more powerful than other mobile phone operating systems. SQL statements we learned in college can basically be used, and the data we created can be operated using ADB shell. The specific path is/data/package_name/databases. For example, here we will demonstrate how to go to the com. Android. providers. Media package.

1, ADB Shell

2, CD/data/COM. Android. providers. Media/databases

3. ls (view the database under com. Android. providers. Media)

4, sqlite3 internal. DB

5,. Help --- see how to operate

6. table lists the tables in the internal data.

7, select * From albums;
Bytes ---------------------------------------------------------------------------------------------
Databasehelper mopenhelper;
Private Static final string database_name = "dbfortest. DB ";
Private Static final int database_version = 1;
Private Static final string table_name = "Diary ";
Private Static final String title = "title ";
Private Static final string body = "body ";
Private Static class databasehelper extends sqliteopenhelper {
Databasehelper (context ){
Super (context, database_name, null, database_version );
}
@ Override
Public void oncreate (sqlitedatabase dB ){
String SQL = "CREATE TABLE" + table_name + "(" + title
+ "Text not null," + body + "text not null" + ");";
Log. I ("Haiyang: createdb =", SQL );
Db.exe csql (SQL );
}
@ Override
Public void onupgrade (sqlitedatabase dB, int oldversion, int newversion ){
}
}
/*
* Recreate a data table
*/
Private void createtable (){
Sqlitedatabase DB = mopenhelper. getwritabledatabase ();
String SQL = "CREATE TABLE" + table_name + "(" + title
+ "Text not null," + body + "text not null" + ");";
Log. I ("Haiyang: createdb =", SQL );
Try {
Db.exe csql ("Drop table if exists Diary ");
Db.exe csql (SQL );
Settitle ("data table reconstruction succeeded ");
} Catch (sqlexception e ){
Settitle ("data table reconstruction error ");
}
}
/*
* Delete A data table
*/
Private void droptable (){
Sqlitedatabase DB = mopenhelper. getwritabledatabase ();
String SQL = "Drop table" + table_name;
Try {
Db.exe csql (SQL );
Settitle ("data table deleted successfully:" + SQL );
} Catch (sqlexception e ){
Settitle ("data table deletion error ");
}
}
/*
* Insert two pieces of data
*/
Private void insertitem (){
Sqlitedatabase DB = mopenhelper. getwritabledatabase ();
String sql1 = "insert into" + table_name + "(" + title + "," + body
+ ") Values ('haiyang', 'android is developing rapidly ');";
String sql2 = "insert into" + table_name + "(" + title + "," + body
+ ") Values ('icesky ', 'android is developing rapidly ');";
Try {
Log. I ("Haiyang: sql1 =", sql1 );
Log. I ("Haiyang: sql2 =", sql2 );
Db.exe csql (sql1 );
Db.exe csql (sql2 );
Settitle ("two data inserted successfully ");
} Catch (sqlexception e ){
Settitle ("failed to insert two data items ");
}
}
/*
* Delete a piece of data.
*/
Private void deleteitem (){
Try {
Sqlitedatabase DB = mopenhelper. getwritabledatabase ();
DB. Delete (table_name, "Title = 'haiyang'", null );
Settitle ("delete a record whose title is Haiyang ");
} Catch (sqlexception e ){
}
}
/*
* The title area of the screen displays the number of data entries in the current data table.
*/
Private void showitems (){
Sqlitedatabase DB = mopenhelper. getreadabledatabase ();
String Col [] = {Title, body };
Cursor cur = dB. Query (table_name, Col, null );
Integer num = cur. getcount ();
Settitle (integer. tostring (Num) + "record ");
}
Bytes ---------------------------------------------------------------------------------------------
Iv. Network

This is to use the Internet to store the data we need, this is the storage method of the CS structure, also click the name.

How to Use content provider

Below are several typical content provider applications that users often access:

* Content provider name: intended data
* Browser: browser bookmarks, browser history, etc.
* Calllog: Missed CILS, call datails, etc.
* Contacts: contact details
* Mediastore: media files such as audio, video and Images
* Settings: device settings and preferences

Call the standard URI structure of the content provider Resource:
Bytes ---------------------------------------------------------------------------------------------
<Standard_prefix >://< authority>/<data_path>/<ID>
Bytes ---------------------------------------------------------------------------------------------
For example:
1) obtain all "bookmarks" in the browser: Content: // Browser/bookmarks
2) Get the information in the system address book: Content: // contacts/People (if you get a specific communication record, specify an ID number: content: // contacts/people/5
Simple instance fragment:
Bytes ---------------------------------------------------------------------------------------------
Uri allcils = URI. parse ("content: // call_log/CILS ");
Cursor c = managedquery (allcils, null );Bytes ---------------------------------------------------------------------------------------------

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.