- ContentProvider负责
- 组织应用程序的数据;
- 向其他应用程序提供数据;
- ContentResolver则负责
- 获取ContentProvider提供的数据;
- 修改/添加/删除更新数据等;
ContentProvider 向外界提供数据
Android提供了ContentProvider,一个程序可以通过实现一个ContentProvider的抽象接口将自己的数据完全暴露出去,而且ContentProviders是以类似数据库中表的方式将数据暴露,也就是说ContentProvider就像一个“数据库”。那么外界获取其提供的数据,也就应该与从数据库中获取数据的操作基本一样,只不过是采用URI来表示外界需要访问的“数据库”。至于如何从URI中识别出外界需要的是哪个“数据库”,这就是Android底层需要做的事情了,不在此详细说。简要分析下ContentProvider向外界提供数据操作的接口:
- query(Uri, String[], String, String[], String)
- insert(Uri, ContentValues)
- update(Uri, ContentValues, String, String[])
- delete(Uri, String, String[])
ContentResolver操作ContentProvider中的数据:
当外部应用需要对ContentProvider中的数据进行添加、删除、修改和查询操作时,可以使用ContentResolver 类来完成,要获取ContentResolver 对象,可以使用Activity提供的getContentResolver()方法。
ContentResolver类提供了与ContentProvider类相同签名的四个方法:
- public Uri insert(Uri uri, ContentValues values)
- public int delete(Uri uri, String selection, String[] selectionArgs)
- public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)
- public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)