标签:
内容提供者是Android中的四大组件之一,可以将应用中的数据对外进行共享内容提供者中数据更改可被监听
内容提供者类 ContentProvider
通过Context获得ContentResolver对象
调用ContentResolver对象的方法即可访问内容提供者
package com.heima.proveders; import com.heima.sqlitedemo.db.personSQLITEOpenHelper; 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 provedersCase extends ContentProvider { private static final String AUTHORITY="com.heima.proveders.provedersCase"; private static final int PERSON_INSERT_CODE = 0;//操作person表添加的操作的uri匹配码 private static final int PERSON_QUERY_ALL_CODE = 3; private static final int PERSON_UPDATE_CODE = 2; private static final int PERSON_DELETE_CODE = 1; private static final int PERSON_QUERY_ITEM_CODE = 4; //UriMatcher类用于匹配Uri private static UriMatcher ruiMatcher; private personSQLITEOpenHelper mOpenHelper = null; static{ //常量UriMatcher.NO_MATCH表示不匹配任何路径的返回码 ruiMatcher = new UriMatcher(UriMatcher.NO_MATCH);//参数是不匹配时返回值的类型 //添加一些uri(分机号) //1唯一标识 手机号 //要干的事 分机号 person/insert是要在person这个表进行insert操作 ruiMatcher.addURI(AUTHORITY, "person/insert", PERSON_INSERT_CODE); ruiMatcher.addURI(AUTHORITY, "person/delete", PERSON_DELETE_CODE); ruiMatcher.addURI(AUTHORITY, "person/update", PERSON_UPDATE_CODE); ruiMatcher.addURI(AUTHORITY, "person/queryAll", PERSON_QUERY_ALL_CODE); //特殊的符号 # ruiMatcher.addURI(AUTHORITY, "person/query/#", PERSON_QUERY_ITEM_CODE); } @Override public boolean onCreate() { // TODO Auto-generated method stub //刚创建的时候就把数据库操作帮助类给实例化出来 mOpenHelper = new personSQLITEOpenHelper(getContext()); return true; } @Override public int delete(Uri arg0, String arg1, String[] arg2) { // TODO Auto-generated method stub int sign = ruiMatcher.match(arg0); switch (sign) { case PERSON_DELETE_CODE: SQLiteDatabase db = mOpenHelper.getWritableDatabase(); if(db.isOpen()){ int count = db.delete("person", arg1, arg2); db.close(); return count; } break; default: throw new IllegalArgumentException("uri不匹配: " + arg1); } return 0; } @Override public String getType(Uri uri) { //这个方法只是为了方便系统的调用,完全不管也是可以的 // TODO Auto-generated method stub int sign = ruiMatcher.match(uri); switch (sign) { case PERSON_QUERY_ALL_CODE: //返回多条的MIME-type return "vnd.android.cursor.dir/person"; case PERSON_QUERY_ITEM_CODE: //查询单个的方法 return "vnd.android.cursor.item/person"; default: //这里不能抛出异常 直接break就可以了 break; } return null; } @Override public Uri insert(Uri uri, ContentValues values) { // TODO Auto-generated method stub //ruiMatcher不仅可以添加uri 还可以匹配uri int sign = ruiMatcher.match(uri); switch (sign) { case PERSON_INSERT_CODE: SQLiteDatabase db = mOpenHelper.getWritableDatabase(); if(db.isOpen()){ //执行操作 long id = db.insert("person", null, values); db.close(); //ContentUris 获取和添加Uri信息 return ContentUris.withAppendedId(uri, id);//往uri后面再加上一个id } break; default: throw new IllegalArgumentException("uri不匹配: " + uri); } return null; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { // TODO Auto-generated method stub int sign = ruiMatcher.match(uri); SQLiteDatabase db = mOpenHelper.getReadableDatabase(); switch (sign) { case PERSON_QUERY_ALL_CODE: if(db.isOpen()){ Cursor cursor = db.query("person", projection, selection, selectionArgs, null, null, sortOrder); return cursor; // db.close(); 这里的cursor不能关闭 一关闭结果集就没了,返回去也就没用了 } break; case PERSON_QUERY_ITEM_CODE: //查询单个值的情况 if(db.isOpen()){ long id = ContentUris.parseId(uri); Cursor cursor = db.query("person", projection, "_id = ?", new String[]{"id"}, null, null, sortOrder); return cursor; } break; default: throw new IllegalArgumentException("uri不匹配: " + uri); } return null; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { // TODO Auto-generated method stub int sign = ruiMatcher.match(uri); switch (sign) { case PERSON_UPDATE_CODE: SQLiteDatabase db = mOpenHelper.getWritableDatabase(); if(db.isOpen()){ int count = db.update("person", values, selection, selectionArgs); db.close(); return count; } break; default: throw new IllegalArgumentException("uri不匹配: " + uri); } return 0; } }内容提供者是四大组件之一,需要在配置清单进行配置
<provider android:name="com.heima.proveders.provedersCase" android:authorities="com.heima.proveders.provedersCase"> </provider>
标签:
原文地址:http://my.oschina.net/u/2356176/blog/420634