注:本文参考 http://blog.sina.com.cn/s/blog_9f233c070101euqx.html
ContentProvider 提供了一个接口用来发布数据,通过ContentResolver 来使用该数据。它们允许将使用数据的应用程序组件和底层数据源分离开来,并提供一种通用的机制来允许一个应用程序共享它们的数据或者使用其它应用程序供的数据。
1、要操作person表中id为10的记录,可以构建这样的路径:/person/10
2、要操作person表中id为10的记录的name字段, person/10/name
3、要操作person表中的所有记录,可以构建这样的路径:/person
4、要操作xxx表中的记录,可以构建这样的路径:/xxx
Uri uri = Uri.parse("content://com.cfox.contentprovid.PersonProvider/person")
首先第一步把你需要匹配Uri路径全部给注册上,如下: //常量UriMatcher.NO_MATCH表示不匹配任何路径的返回码 UriMatcher sMatcher = new UriMatcher(UriMatcher.NO_MATCH); //如果match()方法匹配content://com.cfox.contentprovid.PersonProvider/person路径,返回匹配码为1 sMatcher.addURI("com.cfox.contentprovid.PersonProvider", "person", 1);//添加需要匹配uri,如果匹配就会返回匹配码 //如果match()方法匹配content://com.cfox.contentprovid.PersonProvider/person/230路径,返回匹配码为2 sMatcher.addURI("com.cfox.contentprovid.PersonProvider", "person/#", 2);//#号为通配符 switch (sMatcher.match(Uri.parse("content://com.cfox.contentprovid.PersonProvider/person/10"))) { case 1 break; case 2 break; default://不匹配 break; }注册完需要匹配的Uri后,就可以使用sMatcher.match(uri)方法对输入的Uri进行匹配。如果匹配就返回匹配码,匹配码是调用 addURI()方法传入的第三个参数,假设匹配content://com.cfox.contentprovid.PersonProvider路径,返回的匹配码为1。
Uri uri = Uri.parse("content://com.cfox.contentprovid.PersonProvider/person") Uri resultUri = ContentUris.withAppendedId(uri, 10); //生成后的Uri为:content://com.cfox.contentprovid.PersonProvider/person/10
Uri uri = Uri.parse("content://com.cfox.contentprovid.PersonProvider/person/10") long personid = ContentUris.parseId(uri);//获取的结果为:10
public class BaseContentProvider extends ContentProvider { @Override public boolean onCreate() { // TODO Auto-generated method stub return false; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { // TODO Auto-generated method stub return null; } @Override public String getType(Uri uri) { // TODO Auto-generated method stub return null; } @Override public Uri insert(Uri uri, ContentValues values) { // TODO Auto-generated method stub return null; } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { // TODO Auto-generated method stub return 0; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { // TODO Auto-generated method stub return 0; } }
例如:要得到所有person记录的Uri为content://com.cfox.contentprovid.PersonProvider/person
那么返回的MIME类型字符串应该为:"vnd.android.cursor.dir/person"
如果要操作的数据属于非集合类型数据,那么MIME类型字符串应该以vnd.android.cursor.item/开头
例如:得到id为10的person记录,Uri为content://com.cfox.contentprovid.PersonProvider/person/10
那么返回的MIME类型字符串为:"vnd.android.cursor.item/person"
public String getType(Uri uri) { UriMatcher uriMatcher = getUriMatcher(); int match = uriMatcher.match(uri); switch (match) { case ITEMS: return "vnd.android.cursor.dir/" + person; case ITEMS_ID: return "vnd.android.cursor.item/" + person; default: throw new IllegalArgumentException("Unknown URI: " + uri); } }
<provider android:exported="true" android:name="com.example.contentprovideranddb.ContentProvider.PersonProvider" android:authorities="com.cfox.contentprovid.PersonProvider" />
public Uri insert(Uri uri, ContentValues values):该方法用于往ContentProvider添加数据。
public int delete(Uri uri, String selection, String[] selectionArgs):该方法用于从ContentProvider删除数据。
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs):该方法用于更新ContentProvider中的数据。
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder):该方法用于从ContentProvider中获取数据。
这些方法的第一个参数为Uri,代表要操作的ContentProvider和对其中的什么数据进行操作, 例如 Uri是:Uri.parse("content://com.cfox.contentprovid.PersonProvider/person /10"),那么将会对主机名为com.cfox.contentprovid.PersonProvider的ContentProvider进行操作,操作的数 据为person表中id为10的记录。 使用ContentResolver对ContentProvider中的数据进行添加、删除、修改和查询操作。
public class BaseContentProvider extends ContentProvider { 略......... @Override public Uri insert(Uri uri, ContentValues values) { //添加数据变化通知 getContext().getContentResolver().notifyChange(uri, null); return 返回一个Uri; } 略........ }
public class MainActivity extends Activity { private Handler mHandler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.getContentResolver().registerContentObserver(PersonProvider.uri, false, new MyObserver(mHandler)); } public class MyObserver extends ContentObserver{ public MyObserver(Handler handler) { super(handler); } @Override public void onChange(boolean selfChange) { super.onChange(selfChange); //数据改变时调用 System.out.println("changing ------>"); } } }
<provider android:exported="true" android:name="com.example.contentprovideranddb.ContentProvider.PersonProvider" android:authorities="com.cfox.contentprovid.PersonProvider" />
package com.example.contentprovideranddb.ContentProvider; import android.content.UriMatcher; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import android.provider.BaseColumns; public class PersonProvider extends BaseProvider { public static final String AUTHORITY = "com.cfox.contentprovid.PersonProvider"; public static final String TAB_PATH = "person"; public static final Uri uri = Uri.withAppendedPath(Uri.parse("content://" + AUTHORITY), TAB_PATH); public static UriMatcher mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); static{ mUriMatcher.addURI(AUTHORITY, TAB_PATH, BaseProvider.ITEMS); mUriMatcher.addURI(AUTHORITY, TAB_PATH + "/#", BaseProvider.ITEMS_ID); } public static class Colnums implements BaseColumns{ public static final String name = "name"; public static final String age = "age"; public static final String address = "address"; } public static void createTable(SQLiteDatabase db){ db.execSQL("create table " + TAB_PATH + "(" + Colnums._ID + " integer primary key autoincrement, " + Colnums.name + " text ," + Colnums.age + " integer, " + Colnums.address + " text );"); } @Override public UriMatcher getUriMatcher() { return mUriMatcher; } @Override public String getTablePath() { return TAB_PATH; } @Override public Uri getUri() { return uri; } }DatabaseHelper.java
package com.example.contentprovideranddb.db; import com.example.contentprovideranddb.ContentProvider.PersonProvider; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DatabaseHelper extends SQLiteOpenHelper { private static final String SQL_NAME = "person_info"; private static final int SQL_VERION = 1; private static DatabaseHelper mDatabaseHelper; public DatabaseHelper(Context context) { super(context, SQL_NAME, null, SQL_VERION); } public DatabaseHelper(Context context, String name, int version) { super(context, name, null, version); } public static DatabaseHelper getInstance(Context context) { if (mDatabaseHelper == null) { mDatabaseHelper = new DatabaseHelper(context); } return mDatabaseHelper; } @Override public void onCreate(SQLiteDatabase db) { PersonProvider.createTable(db); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }BaseProvider.java
package com.example.contentprovideranddb.ContentProvider; import com.example.contentprovideranddb.ContentProvider.PersonProvider.Colnums; import com.example.contentprovideranddb.db.DatabaseHelper; 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.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteQueryBuilder; import android.net.Uri; import android.provider.SyncStateContract.Columns; public abstract class BaseProvider extends ContentProvider { public static final int ITEMS = 1; public static final int ITEMS_ID = 2; private SQLiteOpenHelper mSqlitOpenHelper; public abstract UriMatcher getUriMatcher(); public abstract String getTablePath(); public abstract Uri getUri(); @Override public boolean onCreate() { mSqlitOpenHelper = DatabaseHelper.getInstance(getContext()); return true; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteQueryBuilder sqLiteQueryBuilder = new SQLiteQueryBuilder(); UriMatcher uriMatcher = getUriMatcher(); String tablePath = getTablePath(); uriMatcher.addURI(PersonProvider.AUTHORITY, PersonProvider.TAB_PATH, BaseProvider.ITEMS); int match = uriMatcher.match(uri); switch (match) { case ITEMS: sqLiteQueryBuilder.setTables(tablePath); break; case ITEMS_ID: sqLiteQueryBuilder.setTables(tablePath); sqLiteQueryBuilder.appendWhere(Colnums._ID + "=" + uri.getPathSegments().get(1)); break; default: break; } SQLiteDatabase db = mSqlitOpenHelper.getReadableDatabase(); Cursor result = sqLiteQueryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder); if (result == null) { } else { result.setNotificationUri(getContext().getContentResolver(), uri); } return result; } @Override public String getType(Uri uri) { UriMatcher uriMatcher = getUriMatcher(); String tablePath = getTablePath(); int match = uriMatcher.match(uri); switch (match) { case ITEMS: return "vnd.android.cursor.dir/" + tablePath; case ITEMS_ID: return "vnd.android.cursor.item/" + tablePath; default: throw new IllegalArgumentException("Unknown URI: " + uri); } } @Override public Uri insert(Uri uri, ContentValues values) { System.out.println("insert uri--->" + uri); SQLiteDatabase db = mSqlitOpenHelper.getWritableDatabase(); String tablePath = getTablePath(); ContentValues contentValues = null; Uri contentUri = getUri(); if (values != null) { contentValues = new ContentValues(values); } else { contentValues = new ContentValues(); } long rowId = db.insert(tablePath, null, contentValues); Uri resultUri = ContentUris.withAppendedId(contentUri, rowId); //添加数据变化通知 getContext().getContentResolver().notifyChange(uri, null); return resultUri; } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { SQLiteDatabase db = mSqlitOpenHelper.getWritableDatabase(); UriMatcher uriMatcher = getUriMatcher(); String tablePath = getTablePath(); int matcher = uriMatcher.match(uri); switch (matcher) { case ITEMS: return db.delete(tablePath, selection, selectionArgs); case ITEMS_ID: String rowId = uri.getPathSegments().get(1); String where = ""; if (selection == null) { where = Columns._ID + "=" + rowId; } else { where = Columns._ID + "=" + rowId + "AND (" + selection + ")"; } return db.delete(tablePath, where, selectionArgs); default: break; } return 0; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { SQLiteDatabase db = mSqlitOpenHelper.getWritableDatabase(); UriMatcher uriMatcher = getUriMatcher(); String tablePath = getTablePath(); ContentValues contentValues = null; if (values != null) { contentValues = new ContentValues(values); } else { contentValues = new ContentValues(); } int matcher = uriMatcher.match(uri); switch (matcher) { case ITEMS: return db.update(tablePath, contentValues, selection, selectionArgs); case ITEMS_ID: String where = ""; if (selection == null) { String rowId = uri.getPathSegments().get(1); where = Colnums._ID + "=" + rowId; } else { String rowId = uri.getPathSegments().get(1); where = Colnums._ID + "=" + rowId + "AND (" + selection + ")"; } return db.update(tablePath, contentValues, where, selectionArgs); default: break; } return -1; } }
package com.example.contentprovideranddb; import com.example.contentprovideranddb.ContentProvider.PersonProvider; import android.app.Activity; import android.content.ContentUris; import android.content.ContentValues; import android.database.ContentObserver; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.view.View; public class MainActivity extends Activity { private int id = 0; private Handler mHandler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.getContentResolver().registerContentObserver(PersonProvider.uri, false, new MyObserver(mHandler)); } public class MyObserver extends ContentObserver{ public MyObserver(Handler handler) { super(handler); } @Override public void onChange(boolean selfChange) { super.onChange(selfChange); System.out.println("base changing ------>"); } } public void insert(View v) { ContentValues values = new ContentValues(); values.put("name", "zhangesan"); values.put("age", 12); values.put("address", "北京市"); Uri uri = this.getContentResolver().insert(PersonProvider.uri, values); System.out.println("uri----->" + uri); } public void delete(View v) { int rowId = this.getContentResolver().delete(ContentUris.withAppendedId(PersonProvider.uri, id), null, null); System.out.println("delete----->" + rowId); } public void update(View v) { ContentValues values = new ContentValues(); values.put("name", "lisiguang"); values.put("age", 12); values.put("address", "长春市"); int rowId = this.getContentResolver().update(ContentUris.withAppendedId(PersonProvider.uri, id), values, null, null); System.out.println("update----->" + rowId); } public void query(View v) { Cursor cursor = this.getContentResolver().query(PersonProvider.uri, null, null, null, null); while (cursor.moveToNext()) { id = cursor.getInt(cursor.getColumnIndex("_id")); String name = cursor.getString(cursor.getColumnIndex("name")); int age = cursor.getInt(cursor.getColumnIndex("age")); String addr = cursor.getString(cursor.getColumnIndex("address")); System.out.println("id---->" + id + ";name ----->" + name + "; age ---->" + age + "; addr ---->" + addr); } } }