内容提供者:提供数据给别的程序的访问接口
内容解析者:通过内容提供器来读取或操作对应程序的数据
内容提供者步骤:
新建一个类继承ContentProvider创建一个内容提供器,实现他里面的方法
或者在包下面new–Other–Content Provider创建
创建一个静态代码块,创建UriMatcher路径匹配器, 并调用addURI()方法去添加匹配规则
一个完整的uri是 content://com.example.aap.provider/query
uri是由authority和path组成的 第三个参数是一个匹配码
static {
//创建UriMatcher路径匹配器
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
//调用addURI()方法去添加匹配规则
uriMatcher.addURI("com.example.mycontent.provider", "query", QUERYTABLE);
uriMatcher.addURI("com.example.mycontent.provider", "insert", INSERTTABLE);
uriMatcher.addURI("com.example.mycontent.provider", "delete", DELETETABLE);
uriMatcher.addURI("com.example.mycontent.provider", "update", UPDATETABLE);
}
初始化内容提供器,在这个方法对数据库的创建和升级的调用
//
@Override
public boolean onCreate() {
myDatabase = new MyDatabase(getContext());
db = myDatabase.getReadableDatabase();
return true;
}
由于是提供数据给别的程序的访问接口使用,所以直接返回
添加数据
@Override
public Uri insert(Uri uri, ContentValues values) {
int code = uriMatcher.match(uri);
//判断传过来的uri是否与匹配码对应
if (code == INSERTTABLE) {
long insert = db.insert("meno", null, values);
Uri uri1 = Uri.parse(String.valueOf(insert));
//返回值代表新添加数据的id
return uri1;
} else {
throw new UnsupportedOperationException("路径不匹配");
}
}
更新数据
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int code = uriMatcher.match(uri);
if (code == UPDATETABLE) {
int update = db.update("meno", values, selection, selectionArgs);
//返回值代表影响的行数
return update;
}
throw new UnsupportedOperationException("路径不匹配");
}
删除数据
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int code = uriMatcher.match(uri);
if (code == DELETETABLE) {
int delete = db.delete("meno", selection, selectionArgs);
//返回值代表被删除的行数
return delete;
}
throw new UnsupportedOperationException("路径不匹配");
}
查询数据
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
int code = uriMatcher.match(uri);
if (code == QUERYTABLE) {
//路径匹配成功
Cursor cursor = db.query("meno", projection, selection, selectionArgs, null, null, sortOrder);
return cursor;
} else {
throw new UnsupportedOperationException("路径不匹配");
}
}
内容解析者
内容解析者添加
Uri uri1 = Uri.parse("content://com.example.mycontent.provider/insert");
ContentValues values = new ContentValues();
values.put("name", "狐狸");
values.put("age", 21);
Uri insert = getContentResolver().insert(uri1, values);
Toast.makeText(this, "添加了" + insert + "行", Toast.LENGTH_SHORT).show();
内容解析者更新
Uri uri2 = Uri.parse("content://com.example.mycontent.provider/update");
ContentValues values1 = new ContentValues();
values1.put("name", "熊猫");
int update = getContentResolver().update(uri2, values1, "name=?", new String[]{"狐狸"});
Toast.makeText(this, "更新了" + update + "行", Toast.LENGTH_SHORT).show();
内容解析者删除
Uri uri3 = Uri.parse("content://com.example.mycontent.provider/delete");
int delete = getContentResolver().delete(uri3, null, null);
Toast.makeText(this, "删除了" + delete + "行", Toast.LENGTH_SHORT).show();
内容解析者查询
Uri uri4 = Uri.parse("content://com.example.mycontent.provider/query");
Cursor cursor = getContentResolver().query(uri4, null, null, null, null);
if (cursor != null && cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String name = cursor.getString(1);
int age = cursor.getInt(2);
Toast.makeText(this,name+age,Toast.LENGTH_SHORT).show();
}
}