作用
1,使用现有的内容提供器读取或操作相应程序中的数据,比如电话薄、相册等
2,为我们的程序数据提供外部访问接口
基本用法
ContentResolver实例通过Context的getContentResolver()方法获取,然后调用该实例的insert()、update()、delete()、query()方法对数据进行操作。这些方法都需要传入内容URI实例作为参数。
内容URI
内容URI给内容提供器中的数据建立了唯一标识符,它有两部分构成:
- authority:用于对不同程序进行区分,一般以程序包名进行命名。例如一个程序包名为com.example.app,则该程序authority可以命名为com.example.app.provider
- path:用于区分不同的表,通常加到authority后面。例如表名为table1,则path可以命名为/table1。
然后用协议定义这个数据是URI,一般为content://
完整的URI例子:
content://com.example.app.provider/table1
content://com.example.app.provider/table2
URI的实例可以调用Uri.parse()方法得到:
Uri uri= Uri.parse("content://com.example.app.provider/table1");
CRUD操作
- insert(uri,ContentValues实例)
- update(uri,ContentValues实例,where,where中的值)
- delete(uri,where,where中的值)
- query(uri,指定查询的列,where,where中的值,排序)
创建自己的内容提供器
1,创建一个类去继承ContentProvider,并实现他的6个抽象方法。
package com.example.contactstest;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
public class MyProvider extends ContentProvider {
private static UriMatcher uriMatcher;
static {
/**
* UriMatcher类可以轻松实现匹配内容URI的功能。
* UriMatcher实例的addURI()方法接收三个参数:
* authority
* path
* 自定义代码
* 然后调用UriMatcher实例的match()方法,该方法传入一个uri实例,这样就可以判断出调用方期望访问的是哪张表中的数据了
*/
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI("com.example.app.provider","table1",0);
uriMatcher.addURI("com.example.app.provider","table1/#",1);
}
//初始化时调用,通常在这里进行数据库创建和升级等操作,整个则返回true,失败返回false
@Override
public boolean onCreate() {
return false;
}
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] strings, @Nullable String s, @Nullable String[] strings1, @Nullable String s1) {
switch (uriMatcher.match(uri)){
case 0:
//查询table1表中所有数据
break;
case 1:
//查询table1表中单挑数据
break;
default:
break;
}
return null;
}
/**
* 用户获取Uri对象所对应的MIME类型。MIME类型由三部分构成
* 必须以vnd开头
* 如果内容uri以路径结尾,则后接android.cursor.dir/,如果内容uri以id结尾,则后接android.cursor.item/结尾。
* 最后接上vnd.<authority>.<path>
* 例如uri为:content://com.example.app.provider/table1
* 则MIME类型为:vnd.android.cursor.dir/vnd.com.example.app.provider.table1
*
* uri为:content://com.example.app.provider/table1/1
* 则MIME类型为:vnd.android.cursor.item/vnd.com.example.app.provider.table1
*
* @param uri
* @return
*/
@Nullable
@Override
public String getType(@NonNull Uri uri) {
switch (uriMatcher.match(uri)){
case 0:
return "vnd.android.cursor.dir/vnd.com.example.app.provider.table1";
case 1:
//查询table1表中单挑数据
return "vnd.android.cursor.item/vnd.com.example.app.provider.table1";
default:
break;
}
return null;
}
@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues contentValues) {
return null;
}
@Override
public int delete(@NonNull Uri uri, @Nullable String s, @Nullable String[] strings) {
return 0;
}
@Override
public int update(@NonNull Uri uri, @Nullable ContentValues contentValues, @Nullable String s, @Nullable String[] strings) {
return 0;
}
}