Android应用开发之ContentProvider


1、PersonProvider

  1. package cn.class3g.db;  
  2.    
  3. import cn.class3g.service.DatabaseHelper;  
  4. import Android.content.ContentProvider;  
  5. import android.content.ContentUris;  
  6. import android.content.ContentValues;  
  7. import android.content.UriMatcher;  
  8. import android.database.Cursor;  
  9. import android.database.sqlite.SQLiteDatabase;  
  10. import android.net.Uri;  
  11.    
  12. public class PersonProvider extends ContentProvider {  
  13.        private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);  
  14.    
  15.        private static final int PERSONS = 1;  
  16.        private static final int PERSON = 2;  
  17.    
  18.        private DatabaseHelper dbHelper;  
  19.    
  20.        static {  
  21.               matcher.addURI("cn.class3g.providers.personprovider", "person", PERSONS);  
  22.               matcher.addURI("cn.class3g.providers.personprovider", "person/#",  
  23.                             PERSON);  
  24.        }  
  25.    
  26.        public boolean onCreate() {  
  27.               dbHelper = new DatabaseHelper(this.getContext());  
  28.               return true;  
  29.        }  
  30.    
  31.        //  content://cn.itcast.provides.personprovider/person  
  32.        public Uri insert(Uri uri, ContentValues values) {  
  33.               SQLiteDatabase db = dbHelper.getWritableDatabase();  
  34.               long rowId;  
  35.    
  36.               switch (matcher.match(uri)) {  
  37.               case PERSONS: //向表中添加新纪录并返回其行号  
  38.                      rowId = db.insert("person", "personid", values);  
  39.                      return ContentUris.withAppendedId(uri, rowId);  
  40.               default:  
  41.                      throw new IllegalArgumentException("Unknow Uri:" + uri);  
  42.               }  
  43.        }  
  44.    
  45.        public Cursor query(Uri uri, String[] projection, String selection,  
  46.                      String[] selectionArgs, String sortOrder) {  
  47.               SQLiteDatabase db = dbHelper.getReadableDatabase();  
  48.               switch (matcher.match(uri)) {  
  49.               case PERSONS:  
  50.                      return db.query("person", projection, selection, selectionArgs, null, null, sortOrder);  
  51.               case PERSON:  
  52.                      long personid = ContentUris.parseId(uri);  
  53.                      String where = "personid="+ personid;  
  54.                      if(selection!=null && !"".equals(selection)){  
  55.                             wherewhere = where + " and "+ selection;  
  56.                      }  
  57.                      return db.query("person", projection, where, selectionArgs, null, null, sortOrder);  
  58.                       
  59.               default:  
  60.                      throw new IllegalArgumentException("Unknown Uri:"+ uri);  
  61.               }  
  62.        }  
  63.    
  64.        //  content://cn.itcast.provides.personprovider/person 更新表中的所有记录  
  65.        //  content://cn.itcast.provides.personprovider/person/10 更新表中指定id的记录  
  66.        public int update(Uri uri, ContentValues values, String selection,  
  67.                      String[] selectionArgs) {  
  68.               SQLiteDatabase db = dbHelper.getWritableDatabase();  
  69.               int num;  
  70.               switch(matcher.match(uri)){  
  71.               case PERSONS: //更新指定记录  
  72.                      num = db.update("person", values, selection, selectionArgs);  
  73.                      break;  
  74.               case PERSON:  
  75.                      long personid = ContentUris.parseId(uri);  
  76.                      String where = "personid=" + personid;  
  77.                      if(selection != null){  
  78.                             where += " and " + selection;  
  79.                      }  
  80.                      num = db.update("person", values, where, selectionArgs);  
  81.                      break;  
  82.               default:  
  83.                      throw new IllegalArgumentException("Unknow Uri"+uri);  
  84.               }  
  85.               return num;  
  86.        }  
  87.    
  88.        public int delete(Uri uri, String selection, String[] selectionArgs) {  
  89.               SQLiteDatabase db = dbHelper.getWritableDatabase();  
  90.               int num = 0;  
  91.               switch (matcher.match(uri)) {  
  92.               case PERSONS:  
  93.                      num = db.delete("person", selection, selectionArgs);  
  94.                      break;  
  95.               case PERSON:  
  96.                      long personid = ContentUris.parseId(uri);  
  97.                      String where = "personid="+ personid;  
  98.                      if(selection!=null && !"".equals(selection)){  
  99.                             wherewhere = where + " and "+ selection;  
  100.                      }  
  101.                      num = db.delete("person", where, selectionArgs);  
  102.                      break;      
  103.               default:  
  104.                      throw new IllegalArgumentException("Unknown Uri:"+ uri);  
  105.               }  
  106.               return num;  
  107.        }  
  108.    
  109.        public String getType(Uri uri) {  
  110.               switch (matcher.match(uri)) {  
  111.               case PERSONS:  
  112.                      return "vnd.android.cursor.dir/person";  
  113.               case PERSON:  
  114.                      return "vnd.android.cursor.item/person";  
  115.               default:  
  116.                      throw new IllegalArgumentException("Unknown Uri:"+ uri);  
  117.               }  
  118.        }  
  119. }  

2、配置

  1. <provider  
  2.             android:authorities="cn.class3g.providers.personprovider"  
  3.             android:name="PersonProvider" >  
  4.         </provider>  

3、建立测试工程编写测试代码

  1. package cn.class3g.visitor;  
  2.    
  3. import android.content.ContentResolver;  
  4. import android.content.ContentValues;  
  5. import android.database.Cursor;  
  6. import android.net.Uri;  
  7. import android.test.AndroidTestCase;  
  8. import android.util.Log;  
  9.    
  10. public class AccessContentProviderTest extends AndroidTestCase {  
  11.      
  12.     public void testSave() throws Throwable{  
  13.        ContentResolver resolver = this.getContext().getContentResolver();  
  14.        Uri insertUri = Uri.parse("content://cn.class3g.providers.personprovider/person");  
  15.        ContentValues values = new ContentValues();  
  16.        values.put("name", "laozhang");  
  17.        values.put("age", "50");  
  18.        Uri uri = resolver.insert(insertUri, values);  
  19.        Log.i("TAG", uri.toString());  
  20.     }  
  21.      
  22.     public void testQuery() throws Throwable{  
  23.        ContentResolver resolver = this.getContext().getContentResolver();  
  24.        Uri uri = Uri.parse("content://cn.class3g.providers.personprovider/person");  
  25.         
  26.        Cursor cursor = resolver.query(uri, null, null, null, "personid asc");  
  27.         
  28.        while(cursor.moveToNext()){  
  29.            int personid = cursor.getInt(cursor.getColumnIndex("personid"));  
  30.            String name = cursor.getString(cursor.getColumnIndex("name"));  
  31.             
  32.            Log.i("TAG", "personid="+ personid + ",name="+ name);  
  33.        }  
  34.        cursor.close();  
  35.     }  
  36.      
  37.     public void testUpdate() throws Throwable{  
  38.        ContentResolver contentResolver = this.getContext().getContentResolver();  
  39.        Uri updateUri = Uri.parse("content://cn.class3g.providers.personprovider/person/5");  
  40.        ContentValues values = new ContentValues();  
  41.        values.put("name", "蒋介石");  
  42.        contentResolver.update(updateUri, values, null, null);  
  43.     }  
  44.     public void testDelete() throws Throwable{  
  45.        ContentResolver contentResolver = this.getContext().getContentResolver();  
  46.        Uri uri = Uri.parse("content://cn.class3g.providers.personprovider/person/5");  
  47.        contentResolver.delete(uri, null, null);  
  48.     }    
  49. }  

4、测试(注意需要先将provider拥有者工程部署到设备上)

5、ContentProvider的监听器

相关内容