Android Content Provider 使用

Content Provider:一个组件,必须放在应用的主包或应用的子包之下;

组件的配置需要在清单文件中进行配置;content provider需要在application节点中进行配置;
内容提供者在应用中的作用是对外共享数据(任意类型的数据)使用的,别的程序可以对数据进行CRUD,如通讯录;
如果采用文件的方式对外共享数据,会因为文件的类型不同而需要使用不同的api访问方式导致访问繁杂,而内容提供者提供了统一的api对数据进行操作;

<provider
  android:name=".PersonProvider"<!-- 内容提供者类的名称 -->
  android:authorities="cn.wordtech.providers.personprovider" <!--为内容提供者指定一个唯一的标识,这样应用才唯一可以获取此provider-->
  android:exported="false" ><!-- 解决 android Permission Denial error!,在监听内容提供者数据发生变化时需要配置此项 -->
</provider>

Uri 代表了要操作的数据;
Uri主要包含两部分的信息:
1>>需要操作的ContentProvider
2>>对ContentProvider中的什么数据进行操作


ContentProvider(内容提供者)的scheme已经由Android所规定,scheme为:content://
主机名(或Authority)用于唯一标识这个ContentProvider,外部调用者可以根据此标识来找到它,
路径(path)可以用来表示我们要操作的数据,路径的构建根据业务而定。
ex:
要操作person表中id为10的记录,可以构建这样的路径:/person/10
要操作person表中id为10的记录的name字段,可以构建这样的路径:/person/10/name
要操作person表中的所有记录,可以构建这样的路径:/person
要操作XXX表中的记录,可以构建这样的路径:/XXX
要操作的数据不一定是数据库中的文件,也可以是文件,xml或网络等其它方式
ex:
要操作xml文件中person节点下的name节点,可以构建这样的路径:/person/name

下面是一个ContentProvider实例

public class PersonProvider extends ContentProvider {// Content Provider需要继承自ContentProvider类
    // 删改查中,都有两种情况:
    // person 对整个表进行操作
    // person/id 对表中的与id对应记录进行操作
    private DBOpenHelper dbOpenHelper;
    private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);// new UriMatcher(code);code即为匹配不成功时返回的值;
    private static final int PERSONS = 1;
    private static final int PERSON = 2;
    // 设置匹配项
    static {
        MATCHER.addURI("cn.wordtech.providers.personprovider", "person",PERSONS);
        MATCHER.addURI("cn.wordtech.providers.personprovider", "person/#",PERSON);// #号表示数字
    }
    // content://cn.wordtech.providers.personprovider/person
    @Override
    public boolean onCreate() {
        // 由系统调用,当ContentProvider的实例被创建出来的时候被调用,Android开机后,当第一次有应用访问ContentProvider时才创建ContentProvider;
        dbOpenHelper = new DBOpenHelper(getContext(), 1);
        return false;
    }

    // 可以供外部的应用查询数据,返回查询得到的游标对象
    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        switch (MATCHER.match(uri)) {
        case 1:
            return db.query("person", projection, selection, selectionArgs,
                    null, null, sortOrder);
        case 2:
            long rowid = ContentUris.parseId(uri);// 返回要操作的id
            String where = "personid=" + rowid;
            if (selection != null && !"".equals(selection.trim())) {
                where += "and" + selection;
            }
            return db.query("person", projection, where, selectionArgs, null,
                    null, sortOrder);

        default:
            throw new IllegalArgumentException("");
        }
    }

    // 此方法用于返回目前Uri所代表的数据的MIME类型,
    // 如果操作的数据属于集合类型,则MIME字符串就以"vnd.android.cursor.dir"开头
    // 如果操作的数据属于非集合类型,则MIME字符串就以"vnd.android.cursor.item"开头
    @Override
    public String getType(Uri uri) {
        switch (MATCHER.match(uri)) {
        case 1:
            return "vnd.android.cursor.dir/person";
        case 2:
            return "vnd.android.cursor.item/person";
        default:
            throw new IllegalArgumentException("");
        }
    }

    // 此方法需要返回操作记录对应的Uri
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        switch (MATCHER.match(uri)) {
        case 1:
            long rowid = db.insert("person", "", values);// 返回行号?主键值
            // Uri insertUri = Uri
            // .parse("content://com.sqlite.PersonProvider/person/"
            // + rowid);
            Uri insertUri = ContentUris.withAppendedId(uri, rowid);
            return insertUri;
        default:
            throw new IllegalArgumentException("this is Unknow Uri:" + uri);
        }

    }

    // 返回受影响的行数
    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        int num = 0;
        switch (MATCHER.match(uri)) {
        case 1:
            num = db.delete("person", selection, selectionArgs);// 清空整个表
            break;
        case 2:
            long rowid = ContentUris.parseId(uri);// 返回要操作的id
            String where = "personid=" + rowid;
            if (selection != null && !"".equals(selection.trim())) {
                where += "and" + selection;
            }
            num = db.delete("person", where, selectionArgs);
            break;
        default:
            throw new IllegalArgumentException("");
        }
        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 1:
            num = db.update("person", values, selection, selectionArgs);
            break;
        case 2:
            long rowid = ContentUris.parseId(uri);// 返回要操作的id
            String where = "personid=" + rowid;
            if (selection != null && !"".equals(selection.trim())) {
                where += "and" + selection;
            }
            num = db.update("person", values, where, selectionArgs);
            break;
        default:
            throw new IllegalArgumentException("");
        }
        return num;
    }
}

 下面是对前一个类进行测试

public class AccessContentProviderTest extends AndroidTestCase {
    public void testinsert() {
        Uri uri = Uri.parse("content://cn.wordtech.providers.personprovider/person");// 根据标识名得到内容提供者
        ContentResolver cr = this.getContext().getContentResolver(); // This class provides applications access to the content model
        ContentValues values = new ContentValues();
        values.put("name", "Livingstone");
        values.put("phone", "110");
        values.put("amount", "1111111111");
        cr.insert(uri, values);// 在cr的内部会调用内容提供者的值;
    }

    public void testdelete() {
        Uri uri = Uri.parse("content://cn.wordtech.providers.personprovider/person/1");// 根据标识名得到内容提供者
        ContentResolver cr = this.getContext().getContentResolver();
        cr.delete(uri, null, null);
    }

    public void testupdate() {
        Uri uri = Uri.parse("content://cn.wordtech.providers.personprovider/person/2");// 根据标识名得到内容提供者
        ContentResolver cr = this.getContext().getContentResolver();
        ContentValues values = new ContentValues();
        values.put("name", "Livingstone11");
        cr.update(uri, values, null, null);
    }

    public void testquery() {
        Uri uri = Uri.parse("content://cn.wordtech.providers.personprovider/person");// 根据标识名得到内容提供者
        ContentResolver cr = this.getContext().getContentResolver();
        Cursor cursor = cr.query(uri, null, null, null, "personid asc");
        while (cursor.moveToNext()) {
            String name = cursor.getString(cursor.getColumnIndex("name"));
            Log.i("Name", name);
        }
    }
}

监听ContentProvider数据变化情况

如果ContentProvider的访问者需要知道ContentProvider中的数据的变化情况,可以在ContentProvider发生数据变化时调用getContentResolver().notifyChange(uri,null)来通知注册在此URI上的访问者。

public class PersonContentProvider extends ContentProvider[
    public Uri insert(Uri uri,ContentValues values){
        db.insert("person","personid",values);
        getContext().getContentResolver().notifyChange(uri,null);
    }//通知注册在此URI上的访问者,此外注册在insert方法上
}

如果ContentProvider的访问者需要得到数据变化通知,必须使用ContentObserver对数据(数据使用URI描述)进行监听,当监听到数据变化通知时,系统就会调用ContentObserver的onChange()方法。

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.wordtech.providers.personprovider/person");
  this.getContentResolver().registerContentObserver(uri, true, new PersonContentdObserver(new Handler()));
  // 第三个对象为监听对象,当数据发生改变的时候通知此对象做相应的改变
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

 private class PersonContentdObserver extends ContentObserver {

  public PersonContentdObserver(Handler handler) {
   super(handler);
  }

  @Override
  public void onChange(boolean selfChange) {
   Uri uri = Uri.parse("content://cn.wordtech.providers.personprovider/person");
   Cursor cursor = getContentResolver().query(uri, null, null, null,"personid desc limit 1");
   while (cursor.moveToNext()) {
    String name = cursor.getString(cursor.getColumnIndex("name"));
    Log.i("Name", name);
   }
   super.onChange(selfChange);
  } 
 }
}

测试应用:

    Uri uri = Uri.parse("content://cn.wordtech.providers.personprovider/person");// 根据标识名得到内容提供者
    ContentResolver cr = MainActivity.this.getContentResolver();
    ContentValues values = new ContentValues();
    values.put("name", "Livingstone");
    values.put("phone", "1101");
    values.put("amount", "1111111111");
    cr.insert(uri, values);
原文地址:https://www.cnblogs.com/a284628487/p/3009566.html