Introduction of android ContentResolver and ContentProvider

  • 2020-12-26 05:52:57
  • OfStack

Data operations in android include:
The first three data operation modes of file, sqlite3, Preferences, ContectResolver and ContentProvider are only for the data in this application, and the program cannot use these three methods to operate the data in other applications.
ContectResolver and ContentProvider are provided in android to manipulate data from other applications.

1. Usage

One application implements ContentProvider to provide content for other applications to operate on,
One application uses ContentResolver to manipulate other application data, but it can also do so in its own application.

1. Acquisition of ContentResolver

Through the Context class:


public abstract ContentResolver getContentResolver(); 


2. ContentResolver commonly used operation


// Query:   
public final Cursor query(Uri uri, String[] projection,  
           String selection, String[] selectionArgs, String sortOrder);  
// new   
public final Uri insert(Uri url, ContentValues values)      
// update   
public final int update(Uri uri, ContentValues values, String where,  
             String[] selectionArgs)  
// delete   
public final int delete(Uri url, String where, String[] selectionArgs)  

The above operation is actually matched to ContentProvider by Uri, and then performed by ContentProvider.
The parameter meaning of the operation and the parameter meaning of the sqlite functions are one and the same.

2. Realize ContentProvider to provide access to the outside world
The caller ContentResoler uses 1 Uri to find the corresponding ContentProvider to do the actual operation.
1. Uri concept
1 Uri looks like this:


scheme://authorities/path/id 

Such as telephone records:


public static final Uri CONTENT_URI = Uri.parse("content://call_log/calls"); 

a. Different calls according to scheme do not process, commonly used: content, android_resource, file, http, etc
b. authorities is defined by provider, as defined in ES73en. xml
c.path and id are easy to understand.

2. Uri definition
Create your own Uri, such as:

      
content://com.shguo.statistic/sms 

There are two kinds of dir and item in general data (more than one can be defined, of course). Create UriMatcher for ContentProvider and add the two:


String AUTHORITY = "com.shguo.statistics";  
UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);  
sUriMatcher.addURI(AUTHORITY, "sms",   SMS_DIR);   //SMS_DIR = 1  
sUriMatcher.addURI(AUTHORITY, "sms/#", SMS_ITEM); //SMS_ITEM = 2 

contentProvider operates on the basis of whether it is dir or item passed into uri.


switch (sUriMatcher.match(uri))  

Let's do it step by step.

3. Define MIME type,
Overriding the getType method: primarily returns the MIME type of Provider based on uri


public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.shguo.sms";  
ublic static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.shguo.sms"; 

getType () as follows:


switch (sUriMatcher.match(uri)) {  
        case SMS_DIR:  
            return   CONTENT_TYPE;  
        case SMS_ITEM:  
            return CONTENT_ITEM_TYPE;  
        default:  
            throw new IllegalArgumentException("Unknown URI " + uri);  
     } 

4. query, insert, delete, update4 operations.

Specific implementation can use sqlite, file and so on. And operate according to uri.
a. query if item plus query condition id.
where = "_ID=" + uri.getPathSegments().get(1) + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : "";
And then finally we have to add
cursor.setNotificationUri(getContext().getContentResolver(), uri);

b. insert requires uri to be dir only. Returns 1 uri plus id after success.


Uri insertUri = ContentUris.withAppendedId(CONTENT_URI, rowId); 

c. update and delete are about the same as query.


// Query:   
public final Cursor query(Uri uri, String[] projection,  
           String selection, String[] selectionArgs, String sortOrder);  
// new   
public final Uri insert(Uri url, ContentValues values)      
// update   
public final int update(Uri uri, ContentValues values, String where,  
             String[] selectionArgs)  
// delete   
public final int delete(Uri url, String where, String[] selectionArgs)  
0

5. Defined in ES167en.xml
provider element, the main attributes are:


name => ContentProvider The name of the class   
authorities => content type Authorized part of   
multiprocess => true Allows creation in each client process provider Instance, eliminate execution IPC The demand.


Related articles: