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.
1. Content Provider.
- 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.
- At present, the use of a content provider is the standard way for Android to share data across applications.
1.1 Content URI.
- Content URI is a unique resource identifier that the content provider app provides for the client app to access it’s shared data.
- The content URI format is like this schema://authority/path.
- schema: This is the content URI protocol, it’s value is content in general.
- authority: This part is used to distinguish different content provider app to avoid conflict, so generally it is the app package name.
- path: This part is used to distinguish different shared data in one android content provider app. The different paths will return different data.
- Below is an android content URI example.
content://com.example.provider/articles
2. ContentResolver.
- Each android application can be a content provider. For example, android phone contacts, short message system, and android media library.
- 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();
- 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.
- 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.
- 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.
- 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.
- 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.
- query(Uri uri, String columnArray[], String whereClause, String wherePlaceHolderValue[], String orderByClause).
- 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.
- 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());
This is the clearest explanation I found about content provider and content resolver. The picture was really helpful.
Superb >>Thanks…