Android Five Ways to store personal summary

Source: Internet
Author: User

One. File storage

FileOutputStream out = openfileoutput ("Data", context.mode_private);

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

String s;

Writer.writer (s);

Two. Sharepreferences Storage (key-value pair form)

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

The operating modes are generally two types:

Mode_private only allow this process to use

Mode_multi_process (' M?lt?) Multi-process

Method 2. Getsharedpreferences (Operation mode) method in activity class

Method 3. The Getsharedpreferences () method in the Preferencemanager class

Write: Sharedpreferences.editor Editor = preferencemanager.getdefaultsharedpreferences (context). edit ();

Editor.putstring ("key", value);

READ: Sharedpreferences prefs = Preferencemanager.getdefaultsharedpreferences (this);

Prefs.get ("Key", "No value when default")

Three. SQLite

1. First to create an auxiliary class for the Sqliteopenhelper database, there are a A, a, a, two methods in the class

A. OnCreate () is used to create tables, such as

public void OnCreate (Sqlitedatabase db) {

Db.execsql (CREATE TABLE statement)

}

B.onupgrade ()

2. Create an open or existing database

Sqliteopenhelper MSH = new Sqliteopenhelper (content or this,db_name,null,version version);

Sqlitedatabase db = Msh.getwritabledatabase ()

3. Add Data

Contentvalue similar to HashMap

Contentvalues contentvalues = new Contentvalues ();

Contentvalues.put ("key", values);

Db.insert ("Table name", condition or null,contentvalues)

4. Querying data

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

if (Cursor.motofirst ()) {

String data = cursor.getstring (Cursor.getcolumnindex ("Data"));

}while (Cursor.movetonext);

Cursor.close ();

5. Correcting data

Db.update ("table name", Values, "Data=", New string[]{"New Data"});

6. Delete data

Db.update ("Table name", "PAGES>?", New string[]{"500"}); Delete pages greater than 500

Four. Storage of Contentprovide shared data

Note: By using the Getcontentresolver () method in the Context, get to the instance of the class, the URI object to query the data in the table

The Uri can be ContactsContract.CommonDataKinds.Phone.CONTENT_URI (read the permissions of the contact person)

1. Content URI to the data set in the content provider
A unique identifier, which consists mainly of two parts, the permissions (authority) and the Paths (path)

Authority: Package name + provider such as Com.example.app.provider

Path: Table name such as Table1

Header Plus Protocol statement

URI standard format notation: 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. Querying 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 the data

Getcontentresolver (). Update (URI, values, "Column1 =?"). and Column2 =? ", newstring[] {" Text "," 1 "});

5. Delete data

Getcontentresolver (). Delete (URI, "Column1 =?", new string[] {"1"});

Four (2) custom ContentProvider

1. Custom class inherits ContentProvider class, re-onCreate (), query (), insert (), update (), delete (), GetType () method

The customization is not uri.parse (), but the first thing to compare is whether the URI is equal Urimatcher.match (the URI passed in), followed by GetType () to return the MIME type corresponding to the URI object based on the URI equality.

     MIME-type rules:
               1.  must start with VND.
               2.  If the content URI ends with a path, Followed by android.cursor.dir/, if the content URI ends with an ID,
               is followed by android.cursor.item/.
              3.  finally connected to the vnd.< Authority>.<path>. such as Vnd.android.cursor.dir/vnd.com.example.app.provider.table1

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



2. Declaration in the context of manifest.xml

<provider

Android:name = "Like Com.example.databasetest.DatabaseProvider"

Android:anthorities = "Like Com.example.databasetest.provider"

/>

Five.

Storage of data through the network, such as uploading files and downloading files, such as user data upload and download check

Android Five Ways to store personal summary

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.