标签: UriMatcherUri内容提供者ContentProviderAndroid
2013-08-01 08:14 943人阅读 收藏 举报
1.概述
ContentProvider为存储和读取数据提供了统一的接口,实现了程序间的数据共享,而应用程序内部没有必要实现这个功能,直接操作数据库就可以!Android内置的许多数据都是使用ContentProvider形式,供开发者调用的(如视频,音频,图片,通讯录等)。
当应用继承ContentProvider类,并重写该类用于提供数据和存储数据的方法,就可以向其他应用共享其数据,统一了数据访问方式。
2.提供一个ContentProvider
-
-
-
-
- public class PersonProvider extends ContentProvider {
- private SQLiteDatabase db;
- private final static int PERSON = 1;
- private final static int PERSONID = 2;
- private final static int DELPERSONID = 3;
- private final static int PERSONS = 4;
-
- private final static UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
-
- static{
-
-
-
-
-
-
-
- MATCHER.addURI("com.baidu.provider.person", "person", PERSON);
- MATCHER.addURI("com.baidu.provider.person", "persons", PERSONS);
- MATCHER.addURI("com.baidu.provider.person", "person/update/#", PERSONID);
- MATCHER.addURI("com.baidu.provider.person", "person/delete/#", DELPERSONID);
- }
-
- @Override
- public boolean onCreate() {
- DbOpenHelper dbOpenHelper = new DbOpenHelper(this.getContext());
- db = dbOpenHelper.getWritableDatabase();
- return true;
- }
-
-
-
-
-
- @Override
- public Uri insert(Uri uri, ContentValues values) {
-
- switch (MATCHER.match(uri)) {
- case PERSON:
-
- long id = db.insert("person", "name", values);
- uri = ContentUris.withAppendedId(uri, id);
- break;
- default:
- break;
- }
- return uri;
- }
- }
-
- <provider android:name=".PersonProvider" android:authorities="com.baidu.provider.person"></provider>
3.ContentProvider的增删改查
(1).增加操作
-
-
-
-
- public Uri insert(Uri uri, ContentValues values) {
- switch (MATCHER.match(uri)) {
- case PERSON:
- long id = db.insert("person", "name", values);
- uri = ContentUris.withAppendedId(uri, id);
- break;
- default:
- break;
- }
- return uri;
- }
-
- public void insertMethod(){
- ContentResolver resolver = MainActivity.this.getApplicationContext().getContentResolver();
- Uri uri = Uri.parse("content://com.baidu.provider.person/person");
- List<String> list = uri.getPathSegments();
- ContentValues values = new ContentValues();
- values.put("name", "zhang");
- values.put("age", "20");
- Uri resultUri = resolver.insert(uri, values);
- if(ContentUris.parseId(resultUri) > 0){
- show("插入成功!");
- }else{
- show("插入失败!");
- }
- }
- public void show(String info){
- Toast.makeText(this, info, Toast.LENGTH_SHORT).show();
- }
(2).删除操作
-
-
-
-
- public int delete(Uri uri, String selection, String[] selectionArgs) {
- int resultId = 0;
- switch (MATCHER.match(uri)) {
- case DELPERSONID:
- long id = ContentUris.parseId(uri);
- if(selection != null){
- selection += (" and _id="+id);
- }else{
- selection = ("_id="+id);
- }
- resultId = db.delete("person", selection, selectionArgs);
- break;
- }
- return resultId;
- }
-
- public void deleteMethod(){
- ContentResolver resolver = MainActivity.this.getApplicationContext().getContentResolver();
- Uri uri = Uri.parse("content://com.baidu.provider.person/person/delete/6");
- int id = resolver.delete(uri, null, null);
- if(id > 0){
- show("删除成功!");
- }else{
- show("删除失败!");
- }
- }
- public void show(String info){
- Toast.makeText(this, info, Toast.LENGTH_SHORT).show();
- }
(3).更新操作
-
-
-
-
- public int update(Uri uri, ContentValues values, String selection,
- String[] selectionArgs) {
- int resultId = 0;
- switch (MATCHER.match(uri)) {
- case PERSONID:
- long id = ContentUris.parseId(uri);
- if(selection != null){
- selection += (" and _id="+id);
- }else{
- selection = ("_id="+id);
- }
- resultId = db.update("person", values, selection, selectionArgs);
- break;
- default:
- break;
- }
- return resultId;
- }
-
- public void updateMethod(){
- ContentResolver resolver = MainActivity.this.getApplicationContext().getContentResolver();
- Uri uri = Uri.parse("content://com.baidu.provider.person/person/update/5");
- ContentValues values = new ContentValues();
- values.put("name", "li");
- values.put("age", "24");
- int id = resolver.update(uri, values, null, null);
- if(id > 0){
- show("更新成功!");
- }else{
- show("更新失败!");
- }
- }
- public void show(String info){
- Toast.makeText(this, info, Toast.LENGTH_SHORT).show();
- }
(4).查询操作
-
-
-
-
- public Cursor query(Uri uri, String[] projection, String selection,
- String[] selectionArgs, String sortOrder) {
- Cursor cursor = null;
- switch (MATCHER.match(uri)) {
- case PERSONS:
- cursor = db.query("person", projection, selection, selectionArgs, null, null, sortOrder);
- break;
- default:
- break;
- }
- return cursor;
- }
-
- ContentResolver resolver = MainActivity.this.getApplicationContext().getContentResolver();
- Uri uri = Uri.parse("content://com.baidu.provider.persons");
- Cursor cursor = resolver.query(uri, null, null, null, null);
- if(cursor != null){
- list.setAdapter(new SimpleCursorAdapter(MainActivity.this, R.layout.item, cursor, new String[]{"_id", "name", "age"}, new int[]{R.id.id, R.id.name, R.id.age}));
- show("查询成功!");
- }else{
- show("查询失败!");
- }
- public void show(String info){
- Toast.makeText(this, info, Toast.LENGTH_SHORT).show();
- }
4.内容观察者
在listView中,使用内容提供者动态更新数据
- ContentResolver resolver = getContentResolver();
- Uri uri = Uri.parse("content://sms/");
- resolver.registerContentObserver(uri, true, new ContentObserver(new Handler()) {
- @Override
- public void onChange(boolean selfChange) {
- super.onChange(selfChange);
-
-
-
-
- }
- });
5.操作示意图