步骤
- 自定义provider类继承ContentProvider
- 实现ContentProvider的抽象方法
- 在清单文件中配置provider
android:name="com.yu.peoplecontentprovider.PeopleContentProvider"
android:authorities="com.yu.peoplecontentprovider"
android:exported="true" />
static String authorities = "com.yu.peoplecontentprovider";
static final UriMatcher sMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static
{
sMatcher.addURI(authorities, "info", 1);
sMatcher.addURI(authorities, "relation", 2);
sMatcher.addURI(authorities, "info/#", 3);
}
- 通过uri匹配执行对应的增删改查等操作
- 创建数据库以供操作
- 创建测试应用
具体代码
provider的创建
package com.yu.peoplecontentprovider;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
public class PeopleContentProvider extends ContentProvider {
SQLiteDatabase db;
static String authorities = "com.yu.peoplecontentprovider";
static final UriMatcher sMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static
{
sMatcher.addURI(authorities, "info", 1);
sMatcher.addURI(authorities, "relation", 2);
sMatcher.addURI(authorities, "info/#", 3);
}
@Override
public boolean onCreate() {
DBhelper dbHelper = new DBhelper(getContext());
db = dbHelper.getWritableDatabase();
return true;
}
/**
* 通过匹配查询
*/
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
Cursor cursor = null;
int match = sMatcher.match(uri);
switch (match) {
case 1:
cursor = db.query("info", null, selection, selectionArgs, null, null, sortOrder);
break;
case 2:
cursor = db.query("relation", null, selection, selectionArgs, null, null, sortOrder);
break;
case 3:
long id = ContentUris.parseId(uri);
System.out.println("id:"+id);
cursor = db.query("info", null, "_id=?", new String[]{id+""}, null, null, null);
break;
}
return cursor;
}
/**
* 通过uri返回数据的 MIME type
*/
@Override
public String getType(Uri uri) {
int match = sMatcher.match(uri);
switch (match) {
case 1:
return "vnd.android.cursor.dir/info";
case 2:
return "vnd.android.cursor.dir/relation";
case 3:
return "vnd.android.cursor.item/info/#";
}
return null;
}
/**
* 通过匹配插入数据库
*/
@Override
public Uri insert(Uri uri, ContentValues values) {
if (sMatcher.match(uri)== 1) {
db.insert("info", null, values);
}
if (sMatcher.match(uri) == 2) {
db.insert("relation", null, values);
}
return uri;
}
/**
* 通过匹配删除内容
*/
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int rowsAffected=0;
if (sMatcher.match(uri) ==1) {
rowsAffected = db.delete("info", selection, selectionArgs);
}
if (sMatcher.match(uri) == 2) {
rowsAffected = db.delete("relation", selection, selectionArgs);
}
return rowsAffected;
}
/**
* 通过匹配更新数据库
*/
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int rowsAffected=0;
if (sMatcher.match(uri) == 1) {
rowsAffected = db.update("info", values, selection, selectionArgs);
}
if (sMatcher.match(uri) == 2) {
rowsAffected = db.update("relation", values, selection, selectionArgs);
}
return rowsAffected;
}
}
数据库创建帮助类
package com.yu.peoplecontentprovider;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DBhelper extends SQLiteOpenHelper {
public DBhelper(Context context) {
super(context, "people.db", null, 2);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table info(_id integer primary key autoincrement,name text,age integer)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("create table relation(_id integer primary key autoincrement,name text,age integer)");
}
}
测试代码
package com.yu.testcontentprovider;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
ContentResolver resolver;
String baseUri = "content://com.yu.peoplecontentprovider";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resolver = getContentResolver();
}
public void insert(View view) {
ContentValues values = new ContentValues();
values.put("name", "eee");
values.put("age", 564);
resolver.insert(Uri.parse(baseUri+"/relation"), values );
}
public void update(View view) {
ContentValues values = new ContentValues();
values.put("age", 4567);
resolver.update(Uri.parse(baseUri+"/relation"), values , "name=?", new String[]{"eee"});
}
public void delete(View view) {
resolver.delete(Uri.parse(baseUri+"/relation"), "name=?", new String[]{"qqq"});
}
public void query(View view) {
final Cursor cursor = resolver.query(Uri.parse(baseUri+"/relation"), null, null, null, null);
if (cursor != null) {
Toast.makeText(getApplicationContext(), "查询", 0).show();
while (cursor.moveToNext()) {
String name = cursor.getString(1);
int age = cursor.getInt(2);
System.out.println("------------------------------");
System.out.println("name:"+name+",age:"+age);
}
}
}
public void queryId(View view) {
Cursor cursor = resolver.query(Uri.parse(baseUri+"/info/20"), null, null, null, null);
if (cursor != null) {
if (cursor.moveToNext()) {
Toast.makeText(getApplicationContext(), "id查询", 0).show();
String name = cursor.getString(1);
int age = cursor.getInt(2);
System.out.println("------------------------------");
System.out.println("name"+name+",age:"+age);
}
}
}
public void getType(View view) {
System.out.println(resolver.getType(Uri.parse(baseUri+"/info/20")));;
}
}