【15】ContentProvider

2016-06-17  本文已影响0人  嗟嗟嗟

一、什么是ContentProvider

内容提供器,顾名思义,就是封装内容提供给程序使用的一个组件,他可以跨应用提供内容。

二、为什么要使用

因为ContentProvider有自己的封装方法,所以他比其他数据存储方式的接口和调用方式更加统一,还可以隐藏自己的数据结构,提高安全性。

三、如何使用系统提供的ContentProvider

一般通过ContentResolver类来间接调用ContentProvider的方法,不同的资源通过Uri来调用。
Uri的格式类似HTTP中的URI,一般分为协议头——包名——表名——ID

以下为查询系统联系人号码程序代码

ContentResolver resolver = getContentResolver();
Cursor cr = resolver.query(Contacts.CONTENT_URI,new String[] {Contacts._ID,Contacts.DISPLAY_NAME},null,null,null);
if(cr!=null){
    while(cr.moveToNext()){
        int id = cr.getInt(cr.getColumnIndex(Contacts._ID));
        String name = cr.getString(cr.getColumnIndex(Contacts.DISPLAY_NAME));
        Log.i("info","id = " + id);
        Log.i("info","name = " + name);
        Cursor cr1 = resolver.query(Phone.CONTENT_URI,new String[] {Phone.NUMBER},Phone.CONTACT_ID+"="+id,null,null);
        if(cr1!=null){
            while(cr1.moveToNext()){
                String number = cr1.getString(cr1.getColumnIndex(Phone.NUMBER));
                Log.i("info","number = " + number);
            }
            cr1.close();
        }
    }
    cr.close();

四、如何自定义一个ContentProvider

新建MyContentProvider类

public class MyContentProvider extends ContentProvider {

    private SQLiteOpenHelper helper;
    private SQLiteDatabase db;
    private  static UriMatcher uriMatcher;
    public static final String CONTENT = "content://";
    public static final String AUTHORITY = "com.geekband.contentproviderdemo03";
    public static final String USER_TABLE_NAME = "usertb";
    public static final int USER_URI_CODE = 1;
    public static final String URI = "content://com.geekband.contentproviderdemo03/usertb";


    static {
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI(AUTHORITY,USER_TABLE_NAME,USER_URI_CODE);
    }

    @Override
    public boolean onCreate() {
        helper = new MySQLiteHelper(getContext(),"user.db",null,1);
        db = helper.getWritableDatabase();
        return false;
    }

    @Nullable
    @Override
    public Cursor query(Uri uri, String[] strings, String s, String[] strings1, String s1) {

        String tableName = getTableName(uri);
        if(TextUtils.isEmpty(tableName)){
            return null;

        }
        Cursor c = db.query(tableName,strings,s,strings1,null,null,s1);
        return c;

    }

    @Nullable//多个的时候返回IVY,单个的时候返回ITEM
    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(Uri uri, ContentValues contentValues) {
        String tableName = getTableName(uri);
        if(TextUtils.isEmpty(tableName)){
            return null;
        }
        long id = db.insert(tableName,null,contentValues);
        return ContentUris.withAppendedId(uri, id);
    }

    @Override
    public int delete(Uri uri, String s, String[] strings) {
        String tableName = getTableName(uri);
        if(TextUtils.isEmpty(tableName)){
            return 0;
        }
        int count = db.delete(tableName,s,strings);
        return count;
    }

    @Override
    public int update(Uri uri, ContentValues contentValues, String s, String[] strings) {
        String tableName = getTableName(uri);
        if(TextUtils.isEmpty(tableName)){
            return 0;
        }
        int count = db.update(tableName,contentValues,s,strings);
        return count;
    }

    public String getTableName(Uri uri){
        int type = uriMatcher.match(uri);
        String tableName = null;
        switch (type){
            case USER_URI_CODE:
                tableName = USER_TABLE_NAME;
                break;
        }
        return tableName;
    }
}

注册

<provider
            android:authorities="com.geekband.contentproviderdemo03"
            android:name=".MyContentProvider"/>
上一篇下一篇

猜你喜欢

热点阅读