ContentProvider

  AndroidManifest.xml中添加

  <provider android:name=".PersonProvidor" android:authorities="cn.self.myandroid" android:exported="true"/>

  android:exported="true" 为设置权限,否则会报错。

public class PersonProvidor extends ContentProvider{

  private DBopenHelper helper;
  //传递的都不匹配,返回NO_MATCH
  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(对应的provider authorities, 表, 返回值);
    MATCHER.addURI("cn.self.myandroid", "person", PERSONS);

    //#为通配符
    MATCHER.addURI("cn.self.myandroid", "person/#", PERSON);
  }
  @Override
  public boolean onCreate() {
    helper = new DBopenHelper(getContext());
    return false;
  }

  @Override
  public Cursor query(Uri uri, String[] projection, String selection,
    String[] selectionArgs, String sortOrder) {
    SQLiteDatabase db = helper.getReadableDatabase();

    //匹配uri
    switch (MATCHER.match(uri)) {
      case PERSONS:
        return db.query("person", projection, selection, selectionArgs, null, null, sortOrder);
      case PERSON:
        long id = ContentUris.parseId(uri);
        String where = "pid = "+id;

        //selection为外部传递的条件
        if(selection !=null&&!"".equals(selection)){
          where = where +" and "+ selection;
        }
        return db.query("person", projection, where, selectionArgs, null, null, sortOrder);

      default:
        throw new IllegalArgumentException("unknow uri:"+uri);
    }
  }

  @Override
  public String getType(Uri uri) {
    switch (MATCHER.match(uri)) {
      case PERSONS:

        //多条数据用vnd.android.cursor.dir
        return "vnd.android.cursor.dir/person";
      case PERSON:

        //单条数据用vnd.android.cursor.item,如查询uri中有id
        return "vnd.android.cursor.item/person";
      default:
        throw new IllegalArgumentException("unknow uri:"+uri);
    }
  }

  @Override
  public Uri insert(Uri uri, ContentValues values) {
    SQLiteDatabase db = helper.getWritableDatabase();
    switch (MATCHER.match(uri)) {
      case PERSONS:
        long rowid = db.insert("person", "name", values);

        //把行id追加到uri后面
        Uri newUri = ContentUris.withAppendedId(uri, rowid);

        //发出变更通知
        getContext().getContentResolver().notifyChange(uri, null);
        return newUri;

      default:
        throw new IllegalArgumentException("unknow uri:"+uri);
    }

  }

  @Override
  public int delete(Uri uri, String selection, String[] selectionArgs) {
    SQLiteDatabase db = helper.getWritableDatabase();
    int count = 0;
    switch (MATCHER.match(uri)) {
      case PERSONS:
        //更新过的记录数
        count = db.delete("person", selection, selectionArgs);
        return count;
      case PERSON:
        long id = ContentUris.parseId(uri);
        String where = "pid = "+id;
        if(selection !=null&&!"".equals(selection)){
          where = where +" and "+ selection;
        }
        count = db.delete("person", where, selectionArgs);
        return count;

      default:
         throw new IllegalArgumentException("unknow uri:"+uri);
    }
  }

  @Override
  public int update(Uri uri, ContentValues values, String selection,
    String[] selectionArgs) {
    SQLiteDatabase db = helper.getWritableDatabase();
    int count = 0;
    switch (MATCHER.match(uri)) {
      case PERSONS:
        //更新过的记录数
        count = db.update("person", values, selection, selectionArgs);
        return count;
       case PERSON:

        //拆封带id的uri
        long id = ContentUris.parseId(uri);
        String where = "pid = "+id;
        if(selection !=null&&!"".equals(selection)){
          where = where +" and "+ selection;
        }
        count = db.update("person", values, where, selectionArgs);
        return count;

      default:
        throw new IllegalArgumentException("unknow uri:"+uri);
      }

    }

  }

添加变更通知

public class MainActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Uri uri = Uri.parse("content://cn.self.myandroid/person");
    ContentResolver contentResolver = getContentResolver();
    contentResolver.registerContentObserver(uri, true, new PersonObserver(new Handler()));
  }

  class PersonObserver extends ContentObserver{

    public PersonObserver(Handler handler) {
      super(handler);
    }
    @Override
    public void onChange(boolean selfChange) {
      ContentResolver resolver = getContentResolver();
      Uri uri = Uri.parse("content://cn.self.myandroid/person");
      Cursor cursor = resolver.query(uri, null, null, null, "pid desc");
      while(cursor.moveToNext()){
        int pid = cursor.getInt(cursor.getColumnIndex("pid"));
        String name = cursor.getString(cursor.getColumnIndex("name"));
        System.out.println(pid+"===="+name);
      }
    }
  }
}

单元测试类:

public class AccessContentProviderTest extends AndroidTestCase {

  public void testInsert() throws Throwable{
    ContentResolver resolver = getContext().getContentResolver();
    Uri insertUri = Uri.parse("content://cn.self.myandroid/person");
    ContentValues values = new ContentValues();
    values.put("name", "hengheng");
    Uri uri = resolver.insert(insertUri, values);
  }

  public void testUpdate() throws Throwable{
    ContentResolver resolver = getContext().getContentResolver();
    Uri insertUri = Uri.parse("content://cn.self.myandroid/person/3");
    ContentValues values = new ContentValues();
    values.put("name", "hengheng1");
    resolver.update(insertUri, values, null, null);
  }

  public void testDelete() throws Throwable{
    ContentResolver resolver = getContext().getContentResolver();
    Uri uri = Uri.parse("content://cn.self.myandroid/person/3");
    resolver.delete(uri, null, null);
  }

  public void testQuery() throws Throwable{
    ContentResolver resolver = getContext().getContentResolver();
    Uri uri = Uri.parse("content://cn.self.myandroid/person");
    resolver.query(uri, null, null, null, "pid desc");
  }
}

posted @ 2014-06-10 14:55  专属坑儿  阅读(268)  评论(0编辑  收藏  举报