android第十七步采用ContentProvider对外共享数据

继承自ContentProvider

清单文件添加

 <provider android:name="com.example.contentprovider.PersonContentProvider" //包地址
                  android:exported="true"  //是否与其他应用交互
                  android:authorities="zhao.providers.personprovider"></provider> //访问地址

UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH)

MATCHER.addURI("zhao.providers.personprovider", "person", persons); //不带ID
MATCHER.addURI("zhao.providers.personprovider", "person/#", person);//带ID

public class PersonContentProvider extends ContentProvider {

    private DBOpenHelper dbOpenHelper;
    public static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
    private static final int persons =1;
    private static final int person =2;
    static{
        MATCHER.addURI("zhao.providers.personprovider", "person", persons);
        MATCHER.addURI("zhao.providers.personprovider", "person/#", person);
    }
    
    @Override
    public int delete(Uri arg0, String arg1, String[] arg2) {
        SQLiteDatabase database=dbOpenHelper.getWritableDatabase();
        int num = 0;
        switch(MATCHER.match(arg0))
        {
        case persons:
            num = database.delete("person", arg1, arg2);
            break;
        case person:
            long rowid= ContentUris.parseId(arg0);
            String where = "personid="+rowid;
            if(arg1!=null && !"".equals(arg1.trim())){
                where+=where+" and "+arg1.trim();
            }
            num = database.delete("person", where, arg2);
            break;
        }
        return 0;
    }

    @Override
    public String getType(Uri arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Uri insert(Uri arg0, ContentValues arg1) {
        SQLiteDatabase database=dbOpenHelper.getWritableDatabase();
        switch(MATCHER.match(arg0)){
        case persons:
            long rowid = database.insert("person", "name", arg1);
            Uri uri = ContentUris.withAppendedId(arg0, rowid);

            this.getContext().getContentResolver().notifyChange(arg0, null);
            return uri;
            
        default:
            throw new IllegalArgumentException("this is Unknown Uri:"+ arg0);
        }
    }

    @Override
    public boolean onCreate() {
        dbOpenHelper = new DBOpenHelper(this.getContext());
        return true;
    }

    @Override
    public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3,
            String arg4) {
        SQLiteDatabase database=dbOpenHelper.getReadableDatabase();
        switch(MATCHER.match(arg0)){
        case persons:
            return database.query("person", arg1, arg2, arg3,null,null, arg4);
        case person:
            long rowid = ContentUris.parseId(arg0);
            String where = "personid="+rowid;
            if(arg2!=null && "".equals(arg2)){
                where += " and "+arg2;
            }
            return database.query("person", arg1, where, arg3,null,null,arg4);
        }
        return null;
    }

    @Override
    public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
        SQLiteDatabase database = dbOpenHelper.getWritableDatabase();
        int num =0;
        switch(MATCHER.match(arg0)){
        case persons:
            num = database.update("person", arg1, arg2, arg3);
            break;
        case person:
            long rowid = ContentUris.parseId(arg0);
            String where = "personid="+rowid;
            if(arg2!=null && !"".equals(arg2)){
                where+=" and "+arg2;
            }
            num =database.update("person", arg1, where, arg3);
            break;
            
        }
        return num;
    }

}

如何访问这里使用其他应用访问

public void textdelete()
    {
        Uri uri = Uri.parse("content://zhao.providers.personprovider/person/41");
        ContentResolver resolver = this.getContext().getContentResolver();
        resolver.delete(uri, null, null);
    }
    public void testinsert()
    {
        Uri uri = Uri.parse("content://zhao.providers.personprovider/person");
        ContentResolver resolver = this.getContext().getContentResolver();
        ContentValues values = new ContentValues();
        values.put("name", "haha");
        values.put("phone", "123123123");
        values.put("amount", "123123123");
        resolver.insert(uri, values);
    }
    public void testupdate(){
        Uri uri = Uri.parse("content://zhao.providers.personprovider/person/42");
        ContentResolver resolver = this.getContext().getContentResolver();
        ContentValues values = new ContentValues();
        values.put("name", "caomang");
        resolver.update(uri, values, null, null);
    }
    public void testselect(){
        Uri uri = Uri.parse("content://zhao.providers.personprovider/person/42");
        ContentResolver resolver = this.getContext().getContentResolver();
        Cursor cursor = resolver.query(uri, null, null, null, null);
        while(cursor.moveToNext()){
            String name = cursor.getString(cursor.getColumnIndex("name"));
            Log.i("1", name);
        }
        cursor.close();
    }

android第十七步采用ContentProvider对外共享数据,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。