<!-- 定义读取权限 --><permission android:name="com.blog.demo.READ_PEOPLE" /><!-- 定义修改权限 --><permission android:name="com.blog.demo.WRITE_PEOPLE" />在application界面下添加provider节点<!-- com.blog.demo.content作为标识 --><!-- 内容在PeopleContentProvider中实现 --><!-- 设置读写权限 --><!-- 设置export为true,对其他apk开放 --><provider android:authorities="com.blog.demo.content" android:name=".PeopleContentProvider" android:readPermission="com.blog.demo.READ_PEOPLE" android:writePermission="com.blog.demo.WRITE_PEOPLE" android:exported="true" />PeopleContentProvider
PeopleContentProvider继承了ContentProvider,实现了各种操作的接口。首先需要定义我们自己的UriMatcher来解析传递过来的uri,分别定义了“people”和“people/#”两种,在查询时会进行区分。private static final int PEOPLE = 1;private static final int PEOPLE_ID = 2;private static final UriMatcher uriMatcher;static { uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); uriMatcher.addURI(AUTHORITY, "people", PEOPLE); uriMatcher.addURI(AUTHORITY, "people/#", PEOPLE_ID);}@Nullable@Overridepublic String getType(Uri uri) { switch(uriMatcher.match(uri)) { case PEOPLE: // 集合类型 vnd.android.cursor.dir return "vnd.android.cursor.dir/com.blog.demo.people"; case PEOPLE_ID: // 非集合类型 vnd.android.cursor.item return "vnd.android.cursor.item/com.blog.demo.people"; default: throw new IllegalArgumentException("Unsupported URI: " + uri); }}其次我们需要创建一个数据库,在onCreate里面创建。private PeopleSQLiteOpenHelper helper;@Overridepublic boolean onCreate() { helper = new PeopleSQLiteOpenHelper(getContext()); return true;}public class PeopleSQLiteOpenHelper extends SQLiteOpenHelper { public final static String LOGTAG = "PeopleSQLiteOpenHelper"; public final static String DB_NAME = "people.db"; public final static String TABLE_NAME = "people"; public final static int VERSION = 1; public final static String COL_ID = "_id"; public final static String COL_NAME = "name"; public final static String COL_ADDR = "addr"; public final static String COL_AGE = "age"; public final static String TABLE_CREATE = "create table if not exists " + TABLE_NAME + "(" + COL_ID + " integer primary key autoincrement not null," + COL_NAME + " text not null, " + COL_ADDR + " text not null, " + COL_AGE + " integer " + ")"; public PeopleSQLiteOpenHelper(Context context) { this(context, DB_NAME, null, VERSION); } public PeopleSQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); LogUtil.log(LOGTAG, "name = " + name + " version = " + version); } @Override public void onCreate(SQLiteDatabase db) { LogUtil.log(LOGTAG, "onCreate"); db.execSQL(TABLE_CREATE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { LogUtil.log(LOGTAG, "onUpgrade"); }}最后实现各种操作方法@Nullable@Overridepublic Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { LogUtil.log(LOGTAG, "query"); if (uriMatcher.match(uri) == PEOPLE) { // 查询所有people数据 return helper.getReadableDatabase().query(TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder); } else if (uriMatcher.match(uri) == PEOPLE_ID) { // 查询所有指定id的people数据 String id = uri.getPathSegments().get(1); return helper.getReadableDatabase().query(TABLE_NAME, projection, PeopleSQLiteOpenHelper.COL_ID +"=?", new String[]{id}, null, null, sortOrder); } return null;}@Nullable@Overridepublic Uri insert(Uri uri, ContentValues values) { if (uriMatcher.match(uri) == PEOPLE) { long rowid = helper.getWritableDatabase().insert(TABLE_NAME, null, values); if (rowid > 0) { Uri rowUri = ContentUris.withAppendedId(uri, rowid); getContext().getContentResolver().notifyChange(rowUri, null); return rowUri; } throw new SQLException("Failed to insert row into " + uri); } throw new IllegalArgumentException("Unsupported URI: " + uri);}@Overridepublic int delete(Uri uri, String selection, String[] selectionArgs) { if (uriMatcher.match(uri) == PEOPLE) { return helper.getWritableDatabase().delete(TABLE_NAME, selection, selectionArgs); } else if (uriMatcher.match(uri) == PEOPLE_ID) { String id = uri.getPathSegments().get(1); return helper.getWritableDatabase().delete(TABLE_NAME, PeopleSQLiteOpenHelper.COL_ID +"=?", new String[]{id}); } throw new IllegalArgumentException("Unsupported URI: " + uri);}@Overridepublic int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { if (uriMatcher.match(uri) == PEOPLE) { return helper.getWritableDatabase().update(TABLE_NAME, values, selection, selectionArgs); } else if (uriMatcher.match(uri) == PEOPLE_ID) { String id = uri.getPathSegments().get(1); return helper.getWritableDatabase().update(TABLE_NAME, values, PeopleSQLiteOpenHelper.COL_ID +"=?", new String[]{id}); } throw new IllegalArgumentException("Unsupported URI: " + uri);}读取自定义ContentProvider
在AndroidManifest.xml文件中,添加权限。<uses-permission android:name="com.blog.demo.READ_PEOPLE"/><uses-permission android:name="com.blog.demo.WRITE_PEOPLE"/>使用getContentResolver来操作数据// 查询数据private List<People> query() { List<People> list = new ArrayList<People>(); Cursor cursor = getContentResolver().query(Content.People.CONTENT_URI, null, null, null, null); if (cursor != null) { while (cursor.moveToNext()) { int id = cursor.getInt(cursor.getColumnIndex(PeopleColumns.ID)); String name = cursor.getString(cursor.getColumnIndex(PeopleColumns.NAME)); String addr = cursor.getString(cursor.getColumnIndex(PeopleColumns.ADDR)); int age = cursor.getInt(cursor.getColumnIndex(PeopleColumns.AGE)); list.add(new People(id, name, addr, age)); } cursor.close(); } return list;}// 查询数据private People query(int id) { Cursor cursor = getContentResolver().query( ContentUris.withAppendedId(Content.People.CONTENT_URI, id), null, null, null, null); People people = null; if (cursor != null) { if (cursor.moveToNext()) { int rid = cursor.getInt(cursor.getColumnIndex(PeopleColumns.ID)); String name = cursor.getString(cursor.getColumnIndex(PeopleColumns.NAME)); String addr = cursor.getString(cursor.getColumnIndex(PeopleColumns.ADDR)); int age = cursor.getInt(cursor.getColumnIndex(PeopleColumns.AGE)); people = new People(rid, name, addr, age); } cursor.close(); } return people;}// 插入数据private void insert(String name, String addr, String age) { ContentValues insertValues = new ContentValues(); insertValues.put(PeopleColumns.NAME, name); insertValues.put(PeopleColumns.ADDR, addr); insertValues.put(PeopleColumns.AGE, age); getContentResolver().insert(Content.People.CONTENT_URI, insertValues);}// 修改数据private void update(int id, String name, String addr, String age) { ContentValues updateValues = new ContentValues(); updateValues.put(PeopleColumns.NAME, name); updateValues.put(PeopleColumns.ADDR, addr); updateValues.put(PeopleColumns.AGE, age); getContentResolver().update(ContentUris.withAppendedId( Content.People.CONTENT_URI, id), updateValues, null, null);}// 删除数据private void delete(int id) { getContentResolver().delete(ContentUris.withAppendedId( Content.People.CONTENT_URI, id), null, null);}也可以用其他方式去修改和删除数据// 修改数据private void update(int id, String name, String addr, String age) { ContentValues updateValues = new ContentValues(); updateValues.put(PeopleColumns.NAME, name); updateValues.put(PeopleColumns.ADDR, addr); updateValues.put(PeopleColumns.AGE, age); getContentResolver().update(Content.People.CONTENT_URI, updateValues, PeopleColumns.ID + "=?", new String[] { Integer.toString(id) });}// 删除数据private void delete(int id) { getContentResolver().delete(Content.People.CONTENT_URI, PeopleColumns.ID + "=?", new String[] {Integer.toString(id)});}辅助类Contentpublic class Content { public final static class People { public final static Uri CONTENT_URI = Uri.parse("content://com.blog.demo.content/people"); public final static class PeopleColumns { public final static String ID = "_id"; public final static String NAME = "name"; public final static String ADDR = "addr"; public final static String AGE = "age"; } }}
新闻热点
疑难解答