Table of contents
1 Data sharing standard: ContentProvider
1.1 Introduction to ContentProvider
1.3 Use ContentResolver to manipulate data
2.1 Develop a subclass of ContentProvider
2.2 Call method using ContentProvider
3 ContentProvider of the operating system
3.1 Use ContentProvider to manage contacts
3.2 Using ContentProvider to manage multimedia
4 Monitor the data change of ContentProvider
1 Data sharing standard: ContentProvider
1.1 Introduction to ContentProvider
ContentProvider is a way to achieve cross-application data sharing in Android. It can contain a set of public URIs through which other applications can manipulate data in the ContentProvider. ContentProvider can be used to read and write databases, access the file system, and even access the network.
1.2 Introduction to URIs
URI (Uniform Resource Identifier) is a way to identify data in ContentProvider. In ContentProvider, each table or view has a corresponding URI. for example,
content://com.example.app.provider/table1
May correspond to a table in your database.
1.3 Use ContentResolver to manipulate data
ContentResolver is the main interface used to manipulate data in ContentProvider. You can perform data manipulation through the query(), insert(), update(), delete() methods of ContentResolver. The following is an example query operation:
Uri uri = Uri.parse("content://com.example.app.provider/table1");
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(uri, null, null, null, null);
2 Develop ContentProvider
2.1 Develop a subclass of ContentProvider
To create a ContentProvider, you need to inherit the ContentProvider class, and then rewrite some necessary methods, such as onCreate (), query (), insert (), update () and delete (). Here is a simple example:
public class MyContentProvider extends ContentProvider {
@Override
public boolean onCreate() {
// 初始化你的 ContentProvider
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
// 根据 uri 进行查询操作
return null;
}
// 其他的方法...
}
2.2 Call method using ContentProvider
Other applications can operate your ContentProvider through ContentResolver. The following is an example query operation:
Uri uri = Uri.parse("content://com.example.app.provider/table1");
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(uri, null, null, null, null);
3 ContentProvider of the operating system
3.1 Use ContentProvider to manage contacts
The Android system provides a ContactsContract ContentProvider through which you can read and write contact data. The following is an example of querying all contacts:
Uri uri = ContactsContract.Contacts.CONTENT_URI;
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(uri, null, null, null, null);
3.2 Using ContentProvider to manage multimedia
Android system also provides MediaStore ContentProvider, you can use it to access multimedia data on the device. The following is an example of querying all images:
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(uri, null, null, null, null);
4 Monitor the data change of ContentProvider
You can monitor the content change of ContentProvider by using ContentObserver. Here is a simple example:
Uri uri = Uri.parse("content://com.example.app.provider/table1");
ContentResolver resolver = getContentResolver();
ContentObserver observer = new ContentObserver(null) {
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
// 当数据改变时,这个方法会被调用
}
};
resolver.registerContentObserver(uri, true, observer);
When data in your ContentProvider changes, all ContentObservers registered to this URI will be notified.