一、ContentProvider使用
(1)ContentProvider方法
- onCreate:创建ContentProvider后调用
- getType:返回当前Uri所代表数据的MIME类型
- query:用于供外部应用从ContentProvider中查询数据
- insert:用于供外部应用向ContentProvider中增加数据
- delete:用于供外部应用从ContentProvider中删除数据
- update:用于供外部应用更新ContentProvider中数据
(2)SQLiteOpenHelper类的实现
public class DBOpenHelper extends SQLiteOpenHelper {
public DBOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
//创建表student
db.execSQL("create table if not exists animal(" +
"_id integer primary key autoincrement, " +
"name text not null , " +
"age integer not null , " +
"sex text not null)");
db.execSQL("create table if not exists person(" +
"_id integer primary key autoincrement, " +
"name text not null , " +
"age integer not null , " +
"sex text not null)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
switch (oldVersion) {
case 1:
db.execSQL("create table if not exists person(" +
"_id integer primary key autoincrement, " +
"name text not null , " +
"age integer not null , " +
"sex text not null)");
break;
}
}
}
(3)ContentProvider 类的实现
public class MyContentProvider extends ContentProvider {
public DBOpenHelper dbOpenHelper;
public static UriMatcher uriMatcher;
public static final int ANIMAL_DIR = 1001;
public static final int ANIMAL_ITEM = 1002;
public static final int PERSON_DIR = 1003;
public static final int PERSON_ITEM = 1004;
public static final String AUTHORITIES = "com.example.dbsqlite.mycontentprovider";
//对UriMatcher进行了初始化
//id用以区分表中不同的数据记录,如果没有id就返回全部。用到了转义符。#代表任意数字,*代表任意字母。
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTHORITIES, "animal", ANIMAL_DIR);
uriMatcher.addURI(AUTHORITIES, "animal/#", ANIMAL_ITEM);
uriMatcher.addURI(AUTHORITIES, "person", PERSON_DIR);
uriMatcher.addURI(AUTHORITIES, "person/#", PERSON_ITEM);
}
/**
* 该方法在ContentProvider被其它应用第一次访问它时才会被创建
* 返回true表示内容提供器初始化成功
*
* @return
*/
@Override
public boolean onCreate() {
dbOpenHelper = new DBOpenHelper(getContext(), "UserInfo.db", null, 2);
return true;
}
/**
* 用于供外部应用从ContentProvider中查询数据
*
* @param uri
* @param projection
* @param selection
* @param selectionArgs
* @param sortOrder
* @return
*/
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
String id;
Cursor cursor = null;
//获取到SQLiteDatabase的实例
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
//判断用户查询哪张表
switch (uriMatcher.match(uri)) {
case ANIMAL_DIR:
cursor = db.query("animal", projection, selection, selectionArgs, null, null, sortOrder);
break;
case ANIMAL_ITEM:
//调用Uri对象的getPathSegments()方法
//它会将内容URI权限之后的部分以“/”符号进行分割,并把分割后的结果放入到一个字符串列表中,
//那这个列表的第0个位置存放的就是路径,第1个位置存放的就是id
id = uri.getPathSegments().get(1);
cursor = db.query("animal", projection, "id=?", new String[]{id}, null, null, sortOrder);
break;
case PERSON_DIR:
cursor = db.query("person", projection, selection, selectionArgs, null, null, sortOrder);
break;
case PERSON_ITEM:
id = uri.getPathSegments().get(1);
cursor = db.query("person", projection, "id=?", new String[]{id}, null, null, sortOrder);
break;
}
return cursor;
}
/**
* 返回当前Uri所代表数据的MIME类型
* 内容URI所对应的MIME字符串主要由三部分组分,
* Android对这三个部分做了以下格式规定:必须以vnd开头;
* 如果内容URI以路径结尾,则后接android.cursor.dir/,
* 如果内容URI以id结尾,则后接android.cursor.item/;
* 最后接上vnd.< authority>.< path>。
*
* @param uri
* @return
*/
@Nullable
@Override
public String getType(@NonNull Uri uri) {
//与已经注册的Uri进行匹配
switch (uriMatcher.match(uri)) {
case ANIMAL_DIR:
return "vnd.android.cursor.dir/vnd.com.example.dbsqlite.mycontentprovider.animal";
case ANIMAL_ITEM:
return "vnd.android.cursor.item/vnd.com.example.dbsqlite.mycontentprovider.animal";
case PERSON_DIR:
return "vnd.android.cursor.dir/vnd.com.example.dbsqlite.mycontentprovider.person";
case PERSON_ITEM:
return "vnd.android.cursor.item/vnd.com.example.dbsqlite.mycontentprovider.person";
}
return null;
}
/**
* 用于供外部应用向ContentProvider中增加数据
*
* @param uri
* @param values
* @return
*/
@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
Uri uri_return = null;
long id;
//判断向哪张表里添加数据
switch (uriMatcher.match(uri)) {
case ANIMAL_DIR:
case ANIMAL_ITEM:
id = db.insert("animal", null, values);
uri_return = Uri.parse("content://" + AUTHORITIES + "/animal/" + id);
break;
case PERSON_DIR:
case PERSON_ITEM:
id = db.insert("person", null, values);
uri_return = Uri.parse("content://" + AUTHORITIES + "/person/" + id);
break;
}
return uri_return;
}
/**
* 用于供外部应用从ContentProvider中删除数据
*
* @param uri
* @param selection
* @param selectionArgs
* @return
*/
@Override
public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
String id;
int row = 0;
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
switch (uriMatcher.match(uri)) {
case ANIMAL_DIR:
row = db.delete("animal", selection, selectionArgs);
break;
case ANIMAL_ITEM:
id = uri.getPathSegments().get(1);
row = db.delete("animal", "id=?", new String[]{id});
break;
case PERSON_DIR:
row = db.delete("person", selection, selectionArgs);
break;
case PERSON_ITEM:
id = uri.getPathSegments().get(1);
row = db.delete("person", "id=?", new String[]{id});
break;
}
return row;
}
/**
* 用于供外部应用更新ContentProvider中数据
*
* @param uri
* @param values
* @param selection
* @param selectionArgs
* @return
*/
@Override
public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
String id;
int row = 0;
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
switch (uriMatcher.match(uri)) {
case ANIMAL_DIR:
row = db.update("animal", values, selection, selectionArgs);
break;
case ANIMAL_ITEM:
id = uri.getPathSegments().get(1);
row = db.update("animal", values, "id=?", new String[]{id});
break;
case PERSON_DIR:
row = db.update("person", values, selection, selectionArgs);
break;
case PERSON_ITEM:
id = uri.getPathSegments().get(1);
row = db.update("person", values, "id=?", new String[]{id});
break;
}
return row;
}
}
(5)在配置文件中进行注册,并注明属性
<!--authorities设置Provider的权限-->
<!--exported指明该Provider可被其它程序访问-->
<provider
android:name=".MyContentProvider"
android:authorities="ccom.example.dbsqlite.mycontentprovider"
android:exported="true"
android:readPermission="com.example.provider.myprovider" />
(6)ContentResolver方法
- insert:用于往ContentProvider添加数据。
- delete:用于从ContentProvider删除数据。
- query:用于从ContentProvider中获取数据。
- update:用于更新ContentProvider中的数据。
(7)数据的增删改查操作
private ContentValues values;
private ContentResolver resolver;
private Uri uri;
private Uri uri_new;
private Uri uri_return;
public void init() {
values = new ContentValues();
resolver = getContentResolver();
uri = Uri.parse("com.example.dbsqlite.mycontentprovider/animal");
}
public void insert() {
values.put("name", "cat");
values.put("age", 2);
values.put("sex", "male");
uri_return = resolver.insert(uri, values);
values.clear();
values.put("name", "tiger");
values.put("age", 2);
values.put("sex", "male");
uri_return = resolver.insert(uri, values);
String id = uri_return.getPathSegments().get(1);
uri_new = Uri.parse("content://com.example.dbsqlite.mycontentprovider/animal" + id);
values.clear();
values.put("name", "lion");
values.put("age", 4);
values.put("sex", "female");
uri_return = resolver.insert(uri, values);
values.clear();
}
public void delete() {
resolver.delete(uri_new, null, null);
}
public void update() {
values.put("age", 5);
resolver.update(uri, values, "age=?", new String[]{"4"});
}
public void query() {
Cursor cursor = resolver.query(uri, null, null, null, null);
if (cursor != null) {
while (cursor.moveToFirst()) {
String name = cursor.getString(cursor.getColumnIndex("name"));
}
cursor.close();
}
}