Data sharing ContentProvider, ContentResolver

In the sample code: 360 cloud disk: own learning materials ---- Android summed up the project ---- ContentProviderExample.rar

I. Introduction

Introduced earlier, the way of data storage operations can be seen, each program are thread control, and data can not be shared, while ContentProvider to solve this problem, ContentProvider is a bridge between data storage and retrieval of all applications, his role is so as to achieve data sharing among the various programs.
ContentProvider can be understood as a special type of stored data, because ContentProvider is an abstract class inherits from the class must implement: getType, insert, delete, update , query method, it provides a set of interfaces to obtain the above-described operation data.
 
As we learn when new to web development Model1 MVC, the data layer for achieving the additions and deletions to change search entity, but the best package in terms of a business layer data manipulation Service, so avoiding the business layer directly manipulate the data layer while this work to implement Service correspondence and better management. Here leads ContentResolver, its role can be understood to correspond to implement Service operation correspondence ContentProvider.

Get ContentResolver object can be achieved by getContentResolver () method
      
It should be noted that each of the external ContentProvider will provide a common URI (packaged Uri object), if the application has data to share, we need to use these data ContentProvider define a URI, that other applications can then be performed through operations on data passing ContentProvider this URI.

-------------------------------------------------- ------------------------------------------
two, URI Description:

http://www.dubblogs.cc:8751/H264.mp4

URI is the role of all the resources on the Internet for example involved said HTML documents, images, etc., to provide a unique identity.

URI consists of three parts:
the first part is: protocols, such as: http. The first and second portions by a ": //" symbol spaced.
The second part is: there's the resource host domain name or IP address, such as: www.dubblogs.cc: 8751. The second and third portions by a "/" symbol spaced.
The third part is: the specific address of the host resources, such as: H264.mp4.

(The third part of it can be omitted, but the first and the second part must have)

-------------------------------------------------- ------------------------------------------
three, ContentProvider the URI:

content://com.prd.contactprovider/contact/20

ContentProvider the URI also consists of three parts:

1. The protocol part (content: //): This is the android specified content protocol

2. host name portion (com.prd.contactprovider): need to be declared in AndroidManifest.xml with to uniquely identify this ContentProvider, external caller can be found by him under this flag.

3. The path portions (contact / 20): We used to represent the data to be operated, the operation is the contact record id table 20. contact / 20 / phone as the representative phone contact table id field of the record 20. Of course, the data to be operated may also be other documents, XML, or other storage network.

content: // media / internal / images stored on the device URI return all the pictures
content: // contacts / people / 5 ID contact information for a contact record 5
content: // contacts: / people / return this URI You get all the contact information on the device.

-------------------------------------------------- ------------------------------------------
four, the string into Uri

Providing two Uri android operation of tools, and are ContentUris UriMatcher. Let's look at ContentUris.

1.ContentUris

We look at his two commonly used methods

1. The first method withAppendedId (uri, id) This method is used for the path plus the ID portion.

Uri.parse = URI URI ( "Content: //com.test.provider.personprovider/person")
Uri resultUri = ContentUris.withAppendedId (URI,. 5);
URI is generated after @: content: //com.test .provider.personprovider / person / 5

which is equivalent to the result Uri.withAppendedPath (Uri baseUri, String PathSegment)
Uri resultUri = Uri.withAppendedPath (URI, ". 5");

2. the second method: parseId (Uri contentUri) this a method for obtaining the ID portion from the path.

Uri.parse uri = uri ( "Content: //com.test.provider.personprovider/person/5")
Long personid = ContentUris.parseId (uri);
// get the result as personid: 5
 
2.UriMatcher, his role is to match Uri

1. First create a UriMatcher object parameter does not match any path indicates UriMatcher.NO_MATCH return code (-1)
UriMatcher uriMatcher = new new UriMatcher (UriMatcher.NO_MATCH);

2. Then define two integer constant (of course, can be more, depending on how many Uri you register)
Private static int the CONTACTS Final = 1;
Private static int Final CONTACT = 2;

3. Then define a static block of code, his role will be executed when the project started this static code block, by addURI register all Uri, attention addURI in the first argument is part of the domain name, do not add content part of the agreement, the second parameter is the path portion, the third parameter is the return code is a match, the second addURI () method, if the path matches Uri second method in the registered paths, will return 2. Where # is the number used to match a wildcard numbers, it can also be used to match any character *.

static {

// 注册URI路径content://com.prd.contactprovider/contactinfo
matcher.addURI("com.prd.contactprovider", "contactinfo", CONTACTS);

// 注册URI路径content://com.prd.contactprovider/contactinfo/#
matcher.addURI("com.prd.contactprovider", "contactinfo/#", CONTACT);
}

4. uriMatcher.match () method to match the address and return a match value Uri, generally used in getType (Uri uri).


// If a match () method for matching content: //com.changcheng.sqlite.provider.contactprovider/contact path, the matching code is returned. 1
uriMatcher.addURI ( "com.changcheng.sqlite.provider.contactprovider", "Contact", 1); // add the need to match the uri, will return a match if the match code

// If a match () method for matching content: //com.changcheng.sqlite.provider.contactprovider/contact/230 path, returns a code 2
uriMatcher.addURI ( "com.changcheng.sqlite.provider.contactprovider", "Contact / # ", 2); // number # is a wildcard

-------------------------------------------------- ------------------------------------------
five, ContentProvider implementation

ContentProvider class inheritance, will implement onCreate (), getType (), delete (), insert (), query (), update (), six methods.

1.onCreate () method, which is created after ContentProvider will be called when the program first visit to this ContentProvider ContentProvider will be created.

2.getType (Uri uri) method, this method returns the MIME type of the data representative of the current Uri.

Q: What MIME type?

A: When we use the browser, the request data to the server, the server returns many types of data, it may some mp3, some excle file, there may be a word file, the server will need to type these data tell the browser how to tell it, it is to explain the MIME type of the data, so the browser plug-in to select the appropriate MIME type, to read the relevant documents, such as word file MIME type is .word application / msword, if we set the word program , to handle this MIME type of program, then the file will be handed over to word word processing program, which is the MIME type, the MIME type android data represent what is it? If the data type of the operation belongs to the set, such as URI is a table representing the data, then the MIME type should vnd.android.cursor.dir / beginning, for example: To get all the contact data table is the contact table, then he Uri is the content: //com.prd.contactprovier/contact, then return MIME type string be vnd.android.cursor.dir / contact to be operated if the data type is non-set data, for example the table contact a data, then the MIME type string should start with / vnd.android.cursor.item, contact record id is obtained, for example 10, Uri of content: //com.prd.contactprovier/contact/10 MIME type of the returned character we can string should be classified MIME type of URI address vnd.android.cursor.item / contact, the same MIME type of URI can perform the same operation, easy to write code.

3.insert (Uri uri, ContentValues values) method, for, when the external program data to the increase in time ContentProvider call. His first parameter is the Uri object that is used to specify the data to be operated. The second parameter is ContentValues, is to increase the content of the data, we see the use of ContentValues and complete insert () operation.

// save data declaration ContentValues
ContentValues new new ContentValues, tValues, serving = ();
tValues.put ( "name", "Wang"); // name fields in the database table name, the value of Wang
tValues.put ( "icon", 123 ); // icon icon corresponding field in a database table, a value of 123, is an int, of course, may be other types.

Uri url = Uri.parse ( "content: //xjl.prd.contactprovider/contactinfo"); // build ContentProvider Uri, where contactinfo correspondence table
. Uri tResolver = getContentResolver () insert (url, tValues); // The Uri Uri address stored address data, and returns the splicing id
Long id = ContentUris.parseId (tResolver); // save data just returns the id

4.delete (Uri uri, String selection, String [] selectionArgs) method, which is used ContentProvider delete operation, the first parameter is Uri object, namely: the data table to be operated. The second parameter selection portion corresponding to where sql statement. The third parameter corresponds to the value where selectionArgs, such as a SQL statement is used to delete the name listed as the data tom, that is, delete from contact where name = "tom", then the selection corresponds to the name column, selectionArgs corresponds tom. When you can pass the Senate this, his definition of a string value name =? ,? Is a placeholder, then define the value of a string array in the placeholder defined array. Side the following example.

String where = "id=?";
String[] selectionArgs = new String[]{"1"};

getContentResolver().delete(url, where, selectionArgs);//删除 id 为 1 的记录

5.update (Uri uri, ContentValues ​​values, String selection, String [] selectionArgs) method, for updating, these four parameters are introduced in front of, URI specifies the data to be operated. values ​​to store data to be updated. The third parameter and the fourth parameter is used to define the update condition, where portion corresponding to the sql, for retrieving the data to be updated.

ContentValues tUpdateValues=new ContentValues();
tUpdateValues.put("msn", "99999");

String tUpdateWhere = "name=?";
String[] tUpdateSelectionArgs = new String[]{"san1"};

getContentResolver().update(url, tUpdateValues, tUpdateWhere, tUpdateSelectionArgs);

6.query (Uri uri, String [] projection, String selection, String [] selectionArgs, String sortOrder) methods for querying, his return value is a Cursor object. We look at his arguments, the first three or four parameters introduced before. The second parameter specifies the projection is a query that returns the column is an array of strings, say the definition of a string array value is the name and the phone, then return name and phone columns. The fifth parameter is specified arrangement, for example, ascending or descending order, the following example:

Cursor tCursor=getContentResolver().query(url, null, null, null, null);

while (tCursor.moveToNext()) {

String id=tCursor.getString(tCursor.getColumnIndex("id"));
String name=tCursor.getString(tCursor.getColumnIndex("name"));
String msn=tCursor.getString(tCursor.getColumnIndex("msn"));

Log.e(TAG, "id为:"+id+" name为:"+name+" msn为:"+msn);
}

-------------------------------------------------- ------------------------------------------
six, complete code

1. First define the database, do not say here.

2. Custom class ContentProvider

public class ContactProvider extends ContentProvider {

private DBOpenHelper dbOpenHelper;

matcher = new UriMatcher static final UriMatcher private (
UriMatcherNOMATCH);

private static final int CONTACTS = 1;
private static final int CONTACT = 2;
private static final String TABLENAME = "contactinfo";

static {

// 注册URI路径content://com.prd.contactprovider/contactinfo
matcher.addURI("xjl.prd.contactprovider", "contactinfo", CONTACTS);

// 注册URI路径content://com.prd.contactprovider/contactinfo/#
matcher.addURI("xjl.prd.contactprovider", "contactinfo/#", CONTACT);
}

@Override
public boolean onCreate() {

dbOpenHelper = new DBOpenHelper(this.getContext());
return true;
}

/ **
* URI obtained MIME type
* /
@Override
public String getType (Uri URI) {

switch (matcher.match(uri)) {

case CONTACTS:

return "vnd.android.cursor.dir/contactinfo";
case CONTACT:

return "vnd.android.cursor.item/contactinfo";

default:
throw new IllegalArgumentException("URI" + uri + "无法解析!");
}
}

// delete the database
@Override
public int the Delete (Uri uri, String Selection, String [] selectionArgs) {

SQLiteDatabase sqLiteDatabase = dbOpenHelper.getWritableDatabase();
int rowNum = 0;

switch (matcher.match(uri)) {

case CONTACTS:

rowNum = sqLiteDatabase.delete(TABLENAME, selection, selectionArgs);
return rowNum;
case CONTACT:

long id = ContentUris.parseId(uri);
String whereClause = "id=" + id;
if (selection != null) {
whereClause += "and" + selection;
}
rowNum = sqLiteDatabase.delete(TABLENAME, whereClause,
selectionArgs);
return rowNum;

default:
throw new IllegalArgumentException("URI" + uri + "无法解析!");
}
}

/ **
* was added to the database and the data
* /
@Override
public Uri INSERT (Uri URI, ContentValues values) {

SQLiteDatabase sqLiteDatabase = dbOpenHelper.getWritableDatabase();

switch (matcher.match(uri)) {

case CONTACTS:

long id = sqLiteDatabase.insert(TABLENAME, "name", values);
Uri insertUri = ContentUris.withAppendedId(uri, id);

Return inserts;

default:
throw new IllegalArgumentException("URI" + uri + "无法解析!");
}
}

/ **
* data query a database table
* /
@Override
public the Cursor Query (URI Uri, String [] Projection, Selection String,
String [] selectionArgs, the sortOrder String) {

SQLiteDatabase sqLiteDatabase = dbOpenHelper.getWritableDatabase();
switch (matcher.match(uri)) {

case CONTACTS:

return sqLiteDatabase.query(TABLENAME, projection, selection,
selectionArgs, null, null, sortOrder);
case CONTACT:

long id = ContentUris.parseId(uri);
String whereClause = "id=" + id;

if (selection != null) {

whereClause += "and" + selection;
}
return sqLiteDatabase.query(TABLENAME, projection, whereClause,
selectionArgs, null, null, sortOrder);

default:
throw new IllegalArgumentException("URI" + uri + "无法解析!");
}
}

/ **
* update database table records
* /
@Override
public int Update (Uri URI, ContentValues values, Selection String,
String [] selectionArgs) {

SQLiteDatabase sqLiteDatabase = dbOpenHelper.getWritableDatabase();
int rowNum = 0;

switch (matcher.match(uri)) {

case CONTACTS:

rowNum = sqLiteDatabase.update(TABLENAME, values, selection,
selectionArgs);
return rowNum;

case CONTACT:

long id = ContentUris.parseId(uri);
String whereClause = "id=" + id;

if (selection != null) {

whereClause += "and" + selection;
}
rowNum = sqLiteDatabase.update(TABLENAME, values, whereClause,
selectionArgs);
return rowNum;

default:
throw new IllegalArgumentException("URI" + uri + "无法解析!");
}
}
}

/**
* @Description:主页面
*/
public class MainActivity extends Activity {

private static final String TAG = "@@@MainActivity";

/ ** construct ContentProvider Uri, where contactinfo correspondence table * /
Uri Uri.parse URL = ( "Content: //xjl.prd.contactprovider/contactinfo");

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

/ **
* add operations
* /
for (int I = 0; I <= 2; I ++) {

// save data declaration ContentValues
ContentValues new new ContentValues, tValues, serving = ();
tValues.put ( "name", "San" + I); // name fields in the database table name, the value of Wang
tValues.put ( "icon" , 123 + i); // icon icon corresponding field in a database table, a value of 123, is int
// type, of course, may be other types.

/ * The data address holding Uri, Uri address and returns splice * id /
Uri tResolver = getContentResolver () INSERT (URL,, tValues, serving);.
Long id = ContentUris.parseId (tResolver); // save data just returns the id

Log.e (TAG, "just inserted data id is:" + id);
}

showContact ( "After the addition was completed");

/ **
* delete
* /
String tDelWhere = "ID =?";
String [] = tDelSelectionArgs new new String [] { ". 1"};

getContentResolver () delete (url, tDelWhere, tDelSelectionArgs);. // delete the record with id 1

showContact ( "after the operation is completed");

/ **
* modification operation
* /
ContentValues tUpdateValues new new ContentValues = ();
tUpdateValues.put ( "MSN", "99999");

String tUpdateWhere = "name=?";
String[] tUpdateSelectionArgs = new String[] { "san1" };

getContentResolver () Update (URL, tUpdateValues, tUpdateWhere,.
tUpdateSelectionArgs);

showContact ( "Modify operation is completed");
}

public void showContact(String manipulate) {

/ **
* See operation
* /
the Cursor tCursor = getContentResolver ()
.query (URL, null, null, null, null);

while (tCursor.moveToNext()) {

String id = tCursor.getString(tCursor.getColumnIndex("id"));
String name = tCursor.getString(tCursor.getColumnIndex("name"));
String msn = tCursor.getString(tCursor.getColumnIndex("msn"));

Log.e(TAG, manipulate+" id为:" + id + " name为:" + name + " msn为:" + msn);
}
}
}

3.在 AndroidManifest.xml 配置 provider(与 activity 同级)
<provider
android:name="com.xjl.contentprovider.provider.ContactProvider"
android:authorities="xjl.prd.contactprovider" />

注:android:authorities="xjl.prd.contactprovider"

authorities where the value is registered URI: matcher.addURI ( "xjl.prd.contactprovider", "contactinfo", CONTACTS); in the xjl.prd.contactprovider, here must correspond to, or can not manipulate the data.

This property is in an official document to explain:
A List of One or More URI of the Authorities that the Identify the Data Offered by at The Content Provider Multiple Authorities are listed by Separating Their names with A SEMICOLON the To Avoid a Conflicts, Authority names Should use A the Java-style.. convention Naming (SUCH ascom.example.provider.cartoonprovider). Typically, IT's at the name of the implements that at the ContentProvidersubclass at the Provider
There IS NO default. at Least One Authority the MUST BE specified
[operating results in the same project]
@@@ MainActivity
just id is the insertion of data:. 1
@@@ the MainActivity
id data for the newly inserted: 2
@@@ the MainActivity
id data for the newly inserted:. 3
@@@ the MainActivity
====== add operation is completed ===== =
@@@ the MainActivity
ID is: 1 name is: san0 msn is: null
@@@ the MainActivity
id is: 2 name is: san1 msn is: null
@@@ the MainActivity
id is: 3 name is: san2 msn is: null
@@@ the MainActivity
====== ====== delete operation completion
@@ @MainActivity
ID is: 2 name is: san1 msn is: null
@@@ the MainActivity
ID is: 3 name is: san2 msn is: null
@@@ the MainActivity
====== ====== operation is completed and modified
the MainActivity @@@
ID is: 2 name is: san1 msn is: 99999
@@@ the MainActivity
ID is: 3 name is: san2 msn is: null


4. In other programs to access the custom ContentProvider, needs to be set in the shared ContentProvider in android: exported = "true"

<provider
android:name="com.xjl.contentprovider.provider.ContactProvider"
android:authorities="com.xjl.contactprovider"
android:exported="true" />

So that other programs can access the custom ContentProvider, wherein, in the default is true exported sdk 16 or less, the default value is 17 or above false;

 

In the sample code: 360 cloud disk: own learning materials ---- Android summed up the project ---- ContentProviderExample.rar

Guess you like

Origin www.cnblogs.com/zx-blog/p/11835723.html