Android Content Provider And ContentResolver Example

The android content provider is mainly used for data sharing between different applications. It provides a complete set of mechanisms to allow one program to access data in another program, and also to ensure the security of the data being accessed.

read-data-from-android-content-provider

 

1. Content Provider.

  1. By using a content provider, users can choose to share only some part of the data in one application, thus can avoid privacy data leaks in the program.
  2. At present, the use of a content provider is the standard way for Android to share data across applications.

1.1 Content URI.

  1. Content URI is a unique resource identifier that the content provider app provides for the client app to access it’s shared data.
  2. The content URI format is like this schema://authority/path.
  3. schema: This is the content URI protocol, it’s value is content in general.
  4. authority: This part is used to distinguish different content provider app to avoid conflict, so generally it is the app package name.
  5. path: This part is used to distinguish different shared data in one android content provider app. The different paths will return different data.
  6. Below is an android content URI example.
    content://com.example.provider/articles

2. ContentResolver.

  1. Each android application can be a content provider. For example, android phone contacts, short message system, and android media library.
  2. To get data from a content provider, you need to use a ContentResolver instance in your app. Generally the ContentResolver instance can be obtained by Activity‘s getContentResolver() method.
    ContentResolver contentResolver = getContentResolver();
  3. Then you can invoke ContentResolver‘s method to insert, delete, update and query data that another content provider shared. This is something like SQLite database operation.

3. ContentResolver Methods.

  1. Before process data operation, you should first get the content provider URI instance using below method.
    Uri contentUri = Uri.parse("content://com.dev2qa.example.provider/userinfo");

3.1 Insert Data To Content Provider.

  1. insert(Uri providerUri, ContentValues values)
    Uri contentUri = Uri.parse("content://....");
    
    ContentValues contentValues = new ContentValues();
    
    contentValues.put("column1", value1);
    
    contentValues.put("column2", value2);
    
    getContentResolver().insert(contentUri, contentValues);

3.2 Update Content Provider Data.

  1. update(Uri providerUri, ContentValues values, String whereClause, String conditionValueArr[])
    Uri contentUri = Uri.parse("content://....");
    
    ContentValues contentValues = new ContentValues();
    
    contentValues.put("userName", userName);
    
    contentValues.put("password", passwrod);
    
    String whereClause = "id = ?";
    
    String placeHolderValueArr[] = {"1"}
    
    getContentResolver().update(contentUri, contentValues, whereClause , placeHolderValueArr);

3.3 Delete Content Provider Data.

  1. delete(Uri providerUri, String whereClause, String conditionValueArr[])
    Uri contentUri = Uri.parse("content://....");
    
    String whereClause = "id = ?";
    
    String placeHolderValueArr[] = {"1"}
    
    getContentResolver().delete(contentUri, whereClause , placeHolderValueArr);

3.4 Query Content Provider Data.

  1. query(Uri uri, String columnArray[], String whereClause, String wherePlaceHolderValue[], String orderByClause).
  2. The query method returns an android.database.Cursor object, if it is not null, then use it’s moveToFirst() method to move to the first row.
  3. Then loop in the cursor to get each row use it’s moveToNext() method.
    Uri contentUri = Uri.parse("content://....");
    
    Cursor cursor = getContentResolver().query(contentUri , null, null, null, null);
    
    if(cursor!=null)
    {
        cursor.moveToFirst();
    
        // Loop in the cursor to get each row.
        do{
            // Get column 1 value.
            int column1Index= cursor.getColumnIndex("column1");
            String column1Value= cursor.getString(column1Index);
    
            // Get column 2 value.
            int column2Index= cursor.getColumnIndex("column2");
            String column2Value= cursor.getString(column2Index);
        }while(cursor.moveToNext());

2 thoughts on “Android Content Provider And ContentResolver Example”

  1. This is the clearest explanation I found about content provider and content resolver. The picture was really helpful.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.