内容提供者(ContentProvider)主要作用是对外共享数据,如果数据通过内容提供者对外共享了,那么其他应用就可以从内容提供者中查询到数据,并且可更新数据、删除数据、添加数据,如果采用文件的操作模式对外共享数据,数据的访问方式会因为存储方式的不同导致数据的访问方式无法得到统一,不同存储方式文件对外进行共享其访问的ApI是不一样的,如果采用内容提供者对外共享数据就会统一了数据的访问方式。采用统一的API访问共享的数据。
创建内容提供者步骤
1.创建内容提供者需要继承android.content.ContentProvider
2.清单文件中进行配置:
<!--android:name:指定内容提供者的类名,android:authorities:调用内容..名称,随意取 -->
<provider android:name=".PersonProvider" android:authorities="cn.test.providers.personprovider"/>
ContentProvider类主要方法
public boolean onCreate()
该方法在ContentProvider创建后就会被调用, Android开机后, ContentProvider在其它应用第一次访问它时才会被创建。
public Uriinsert(Uri uri, ContentValues values)
该方法用于供外部应用往ContentProvider添加数据。
public int delete(Uri uri, String selection,String[] selectionArgs)
该方法用于供外部应用从ContentProvider删除数据。
public int update(Uri uri, ContentValues values, Stringselection, String[] selectionArgs)
该方法用于供外部应用更新ContentProvider中的数据。
public Cursorquery(Uri uri, String[]projection, String selection, String[] selectionArgs, String sortOrder)
该方法用于供外部应用从ContentProvider中获取数据。
示例:
内容提供者类,实现数据的增删改查
public class PersonProvider extends ContentProvider { //创建工具类实现Uri匹配 private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH); private static final int PERSONS = 1; private static final int PERSON = 2; static{ MATCHER.addURI("cn.test.providers.personprovider", "person", PERSONS); MATCHER.addURI("cn.test.providers.personprovider", "person/#", PERSON); } private DBOpenHelper dbOpenHelper = null; @Override public boolean onCreate() { dbOpenHelper = new DBOpenHelper(getContext()); return true; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteDatabase db = dbOpenHelper.getReadableDatabase(); switch (MATCHER.match(uri)) { case PERSONS: // 获取person表中的所有数据 /person return db.query("person", projection, selection, selectionArgs, null, null, sortOrder); case PERSON: // 获取person表中的指定id的数据 /person/20 long id = ContentUris.parseId(uri); String where = "personid="+ id; if(selection!=null && !"".equals(selection.trim())){ where += " and "+ selection; } return db.query("person", projection, where, selectionArgs, null, null, sortOrder); default: throw new IllegalArgumentException("Unknown Uri:"+ uri); } } @Override public String getType(Uri uri) { // TODO Auto-generated method stub return null; } @Override public Uri insert(Uri uri, ContentValues values) { //取得数据库操作实例 SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); switch (MATCHER.match(uri)) { case PERSONS: //执行添加,返回行号,如果主健字段是自增长的,那么行号会等于主键id long rowid = db.insert("person", "name", values); //得到拼好的uri Uri insertUri = ContentUris.withAppendedId(uri, rowid); //发出数据变化通知(person表的数据发生变化) getContext().getContentResolver().notifyChange(uri, null); return insertUri; default: //不能识别uri throw new IllegalArgumentException("Unknown Uri:"+ uri); } } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); //受影响的行数 int num = 0; switch (MATCHER.match(uri)) { case PERSONS: // 删除person表中的所有数据 /person num = db.delete("person", selection, selectionArgs); break; case PERSON: // 删除person表中的指定id的数据 /person/20 long id = ContentUris.parseId(uri); String where = "personid="+ id; if(selection!=null && !"".equals(selection.trim())){ where += " and "+ selection; } num = db.delete("person", where, selectionArgs); break; default: throw new IllegalArgumentException("Unknown Uri:"+ uri); } return num; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); int num = 0; switch (MATCHER.match(uri)) { case PERSONS: // 更新person表中的所有数据 /person num = db.update("person", values, selection, selectionArgs); break; case PERSON: // 更新person表中的指定id的数据 /person/20 long id = ContentUris.parseId(uri); String where = "personid="+ id; if(selection!=null && !"".equals(selection.trim())){ where += " and "+ selection; } num = db.update("person", values, where, selectionArgs); break; default: throw new IllegalArgumentException("Unknown Uri:"+ uri); } return num; } }
其他工程中访问:
public class AccessContentProiderTest extends AndroidTestCase { public void testInsert() throws Throwable{ ContentResolver resolver = getContext().getContentResolver(); Uri uri = Uri.parse("content://cn.test.providers.personprovider/person"); ContentValues values = new ContentValues(); values.put("name", "lili"); values.put("phone", "110"); values.put("amount", "3000000000"); resolver.insert(uri, values); } public void testDelete() throws Throwable{ ContentResolver resolver = getContext().getContentResolver(); Uri uri = Uri.parse("content://cn.test.providers.personprovider/person"); int num =resolver.delete(uri, null, null); } public void testUpdate() throws Throwable{ ContentResolver resolver = getContext().getContentResolver(); Uri uri = Uri.parse("content://cn.test.providers.personprovider/person/65"); ContentValues values = new ContentValues(); values.put("amount", 500); resolver.update(uri, values, null, null); } public void testQuery() throws Throwable{ ContentResolver resolver = getContext().getContentResolver(); Uri uri = Uri.parse("content://cn.test.providers.personprovider/person/65"); Cursor cursor = resolver.query(uri, null, null, null, "personid asc"); while(cursor.moveToNext()){ String name = cursor.getString(cursor.getColumnIndex("name")); Log.i("AccessContentProviderTest", name); } } }