Five storage methods for Android: five storage methods for android

Source: Internet
Author: User

Five storage methods for Android: five storage methods for android

I. File Storage

FileOutputStream out = openFileOutput ("data", Context. MODE_PRIVATE );

BufferedWriter writer = new BufferedWriter (new OutputStream (out ));

String s;

Writer. writer (s );

Ii. Storage of sharePreferences (key-value pairs)

Method 1. getSharedPreferences ("file name", operation mode) method in the Context class

There are two operation modes:

MODE_PRIVATE can only be used by this process

MODE_MULTI_PROCESS ('m has lt wait) multiple processes

Method 2. getSharedPreferences (Operation Mode) method in the Activity Class

Method 3: getSharedPreferences () method in the PreferenceManager class

Write: SharedPreferences. Editor editor = PreferenceManager. getdefasharsharedpreferences (context). edit ();

Editor. putString ("key", value );

Read: SharedPreferences prefs = PreferenceManager. getdefasharsharedpreferences (this );

Prefs. get ("key", "default when no value exists ")

Iii. SQLite

1. Create an auxiliary class for the SQLiteOpenHelper database. There are two methods in the class: a and B.

A. onCreate () is used to create a table, as shown in figure

Public void onCreate (SQLiteDatabase db ){

Db.exe cSQL (Table creation Statement)

}

B. onUpgrade ()

2. Create an open or existing database

SQLiteOpenHelper msh = new SQLiteOpenHelper (content or this, DB_name, null, Version );

SQLiteDatabase db = msh. getwritableDatabase ()

3. Add data

// ContentValue is similar to hashMap

ContentValues contentValues = new ContentValues ();

ContentValues. put ("key", values );

Db. insert ("table name", condition or null, contentValues)

4. query data

Cursor cursor = db. query ("table name", null, null );

If (cursor. moToFirst ()){

String data = cursor. getString (cursor. getColumnIndex ("data "));

} While (cursor. moveToNext );

Cursor. close ();

5. correct data

Db. update ("table name", values, "data =? ", New String [] {" new data "});

6. delete data

Db. update ("table name", "pages>? ", New String [] {" 500 "}); // delete a table with pages greater than 500

4. Storage of ContentProvide shared data

Note: The instance of this class is obtained through the getContentResolver () method in Context, and the Uri object is used to query the data in the table.

Uri can be ContactsContract. CommonDataKinds. Phone. CONTENT_URI (permission to read contacts)

1. The content URI is used to establish data in the content provider.
A unique identifier, which consists of two parts: authority and path)

Authority: package name + provider such as com. example. app. provider

Path: Table name, such as table1

Header with protocol statement

URI Standard Format: content: // com. example. app. provider/table1

Uri uri = Uri. parse ("content: // com. example. app. provider/table1 ")

2. Create data

ContentValues values = new ContentValues ();
Values. put ("column1", "text ");
GetContentResolver (). insert (uri, values );

3. query data

Cursor cursor = getContentResolver (). query (uri, projection, selection, selectionArgs, sortOrder );

If (cursor! = Null ){
While (cursor. moveToNext ()){
String column1 = cursor. getString (cursor. getColumnIndex ("column1 "));

}

Cursor. close ();
}

4. modify data

GetContentResolver (). update (uri, values, "column1 =? And column2 =? ", NewString [] {" text "," 1 "});

5. delete data

GetContentResolver (). delete (uri, "column1 =? ", New String [] {" 1 "});

IV. (2) custom ContentProvider

1. The custom class inherits the ContentProvider class and reuses the onCreate (), query (), insert (), update (), delete (), getType () methods.

Uri is not required for customization. parse (), but first compare whether the uri is equal to uriMatcher. match (the passed uri). Secondly, getType () is the MIME type corresponding to the uri object, which is equal to the Uri.

MIME Type rules:
1. It must start with vnd.
2. If the content URI ends with a path, it is followed by android. cursor. dir/. If the content URI ends with an id,
Then connect to android. cursor. item /.
3. Connect vnd. <authority>. <path>. For example, vnd. android. cursor. dir/vnd.com. example. app. provider. table1

Vnd. android. cursor. item/vnd. com. example. app. provider. table1



2. manifest. xml Declaration

<Provider

Android: name = "such as com. example. databasetest. DatabaseProvider"

Android: anthorities = "for example, com. example. databasetest. provider"

/>

 

5.

Storage of data through the network space, such as uploading and downloading files, including uploading and downloading user data

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.