是什么ContentProvider,有什么用?
为别的应用程序共享(
取用和存储
)数据(
文件数据或SQLite数据等
)提供的
的
一套标准方法
类
。
Android系统自带一些内容提供者,
Browser,
CallLog,
Contacts,
Images,
MediaStore,
Settings,
Video等
如何使用?
程序中写MyContentProvider继承ContentProvider 基类(需要设置一个URI 来唯一标识其数据集)
然后写具体的为其它应用程序取用和存储它管理的数据的方法。
public class MyContentProvider extends ContentProvider {
public MyContentProvider() {
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// Implement this to handle requests to delete one or more rows.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public String getType(Uri uri) {
// TODO: Implement this to handle requests for the MIME type of the data
// at the given URI.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO: Implement this to handle requests to insert a new row.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public boolean onCreate() {
// TODO: Implement this to initialize your content provider on startup.
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO: Implement this to handle query requests from clients.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO: Implement this to handle requests to update one or more rows.
throw new UnsupportedOperationException("Not yet implemented");
}
}
在别的应用程序中不是直接调用这些方法的,而要通过一个 ContentResolver 对象来调用它的方法作为替代。
ContentResolver可以与任意内容提供者进行会话,与其合作来对所有相关交互通讯进行管理。