我的android 第26天 - Contant Provider
?
我的android 第26天 - Contant Provider
?
? ?介绍:Contant?Provider?内容提供者,android四大组件之一,使用于多个应用同时使用同一份数据。它对不同的数据存储(file\sqlite\intent)进行了封装,提供了统一的数据访问方式.android系统内部很多内容都以内容提供者的形式共享输出,比如联系人信息、短信等
?案例:采用Contant?Provider?共享SQLite person表的数据,
??实现增删改查等操作
一、Contant?Provider机制
?
二、采用内容提供者共享数据实现过程
1、继承ContantProvider,重写以下方法
?boolean onCreate():创建ContantProvider时调用?Uriinsert(Uri uri, ContentValues values)插入操作
?int delete(Uri uri,String selection, String[] selectionArgs):删除操作? intupdate(Uri uri, ContentValues values, String selection, String[]selectionArgs):更新操作? Cursor query(Uri uri,String[] projection, String selection, String[] selectionArgs, String sortOrder):查询操作?StringgetType(Uri uri):返回数据的MINE-TYPE?2、内容提供者为android四大组件之一,需要在AndroidManifest.xml中注册
?
?
?
public class PersonProvider extends ContentProvider {static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);private static final int MULTI = 1;private static final int SINGLE = 2;static {// content://cn.itcast.sqlite.providers.personprovider/personmatcher.addURI("cn.itcast.sqlite.providers.personprovider", "person", MULTI);// content://cn.itcast.sqlite.providers.personprovider/person/4matcher.addURI("cn.itcast.sqlite.providers.personprovider", "person/#", SINGLE);}DBHelper helper;/** * 初始化ContentProvider的时候调用 */public boolean onCreate() {helper = new DBHelper(getContext());return false;}@Overridepublic Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder) {SQLiteDatabase db = helper.getReadableDatabase();switch (matcher.match(uri)) {case MULTI: //访问多条数据 content://cn.itcast.sqlite.providers.personprovider/personreturn db.query(PersonMgr.TABLE, projection, selection, selectionArgs, null, null, sortOrder);case SINGLE: // 访问单条数据 content://cn.itcast.sqlite.providers.personprovider/person/4// 解析Uri尾部的idlong id = ContentUris.parseId(uri);String where = PersonMgr.ID + " = " + id;if (selection!=null && !"".equals(selection.trim())) {where += " and " + selection;}return db.query(PersonMgr.TABLE, projection, where, selectionArgs, null, null, sortOrder);case UriMatcher.NO_MATCH: // uri不合法throw new IllegalArgumentException("uri不合法");}return null;}/** * 返回数据的MINE-TYPE : image/jpeg plain/text image/png */public String getType(Uri uri) {switch (matcher.match(uri)) {case MULTI: //多条数据 content://cn.itcast.sqlite.providers.personprovider/personreturn "vnd.android.cursor.dir/person";case SINGLE: // 单条数据 content://cn.itcast.sqlite.providers.personprovider/person/4return "vnd.android.cursor.item/person";case UriMatcher.NO_MATCH: // uri不合法throw new IllegalArgumentException("uri不合法");}return null;}@Overridepublic Uri insert(Uri uri, ContentValues values) {// content://cn.itcast.sqlite.providers.personprovider/personSQLiteDatabase db = helper.getWritableDatabase();switch (matcher.match(uri)) {case MULTI: // content://cn.itcast.sqlite.providers.personprovider/personlong id = db.insert(PersonMgr.TABLE, null, values); db.close();// 发出数据改变的通知getContext().getContentResolver().notifyChange(uri, null);return ContentUris.withAppendedId(uri, id);default: // uri不合法throw new IllegalArgumentException("uri不合法");}}@Overridepublic int delete(Uri uri, String selection, String[] selectionArgs) {SQLiteDatabase db = helper.getReadableDatabase();int result = 0;switch (matcher.match(uri)) {case MULTI: //多条数据 content://cn.itcast.sqlite.providers.personprovider/personresult = db.delete(PersonMgr.TABLE, selection, selectionArgs);break;case SINGLE: // 单条数据 content://cn.itcast.sqlite.providers.personprovider/person/4String where = PersonMgr.ID + "=" + ContentUris.parseId(uri);if (selection != null && !"".equals(selection.trim())) {where += " and " + selection;}result = db.delete(PersonMgr.TABLE, where, selectionArgs);break;case UriMatcher.NO_MATCH: // uri不合法throw new IllegalArgumentException("uri不合法");}db.close();return result;}public int update(Uri uri, ContentValues values, String selection,String[] selectionArgs) {SQLiteDatabase db = helper.getReadableDatabase();int result = 0;switch (matcher.match(uri)) {case MULTI: //多条数据 content://cn.itcast.sqlite.providers.personprovider/personresult = db.update(PersonMgr.TABLE, values, selection, selectionArgs);break;case SINGLE: // 单条数据 content://cn.itcast.sqlite.providers.personprovider/person/4String where = PersonMgr.ID + "=" + ContentUris.parseId(uri);if (selection != null && !"".equals(selection.trim())) {where += " and " + selection;}result = db.update(PersonMgr.TABLE, values, where, selectionArgs);break;case UriMatcher.NO_MATCH: // uri不合法throw new IllegalArgumentException("uri不合法");}db.close();return result;}}
三、关注点
?.?理解Android?ContantProvider?机制
?. Uri格式
?
?
?.?运用UriMatcher匹配Uri
?
?.运用ContentUris为Uri添加或截取尾部的ID数据
???ContentUris.withAppendedId(uri, 10):给Uri尾部添加10(id)
???????ContentUris.parseId(uri);//获取尾部的id
???.?如何监听ContentProvider数据的改变
???.?getType:返回所访问数据的minetype(image/jpeg plain/text??
??????image/png)
?
?
下载视频代码
?
??
下载视频代码