android学习ContentProvider。
1.ContentProvider主要用于对外共享数据。其他应用可以通过uri进行数据的访问甚至是增删改查等操作。
以下小例进行简要说明
publicclassPersonProviderextendsContentProvider{ //DBOpenHelper工具类,继承于SQLiteOpenHelper此文省略代码 privateDBOpenHelperhelper; privatestaticfinalUriMatcherMATCHER=newUriMatcher( UriMatcher.NO_MATCH); privatestaticfinalintPERSONS=1; privatestaticfinalintPERSON=2; /** *在AndroidManifest.xml中注册provider * */ static{ MATCHER.addURI("com.example.providers.personprovider","person",PERSONS); MATCHER.addURI("com.example.providers.personprovider","person/#",PERSON); } /** *删除操作 */ @Override publicintdelete(Uriuri,Stringselection,String[]selectionArgs){ SQLiteDatabasedb=helper.getWritableDatabase(); intnum=0; switch(MATCHER.match(uri)){ case1: db.delete("person",selection,selectionArgs); break; case2: longrowid=ContentUris.parseId(uri); Stringwhere="personid="+rowid; if(selection!=null&&!"".equals(selection.trim())){ where+="and"+selection; } num=db.delete("person",where,selectionArgs); break; default: thrownewIllegalArgumentException("thisidUnknownUri:"+uri); } returnnum; } /** *该方法返回当前uri所代表数据的MIME类型, *若操作的数据属于集合类型,那么MIME类型字符串应该以vnd.android.cursor.dir开头 *若操作的数据属于非集合类型,那么MIME类型字符串应该以vnd.android.cursor.item开头 */ @Override publicStringgetType(Uriuri){ switch(MATCHER.match(uri)){ case1: return"vnd.android.cursor.dir/person"; case2: return"vnd.android.cursor.item/person"; default: thrownewIllegalArgumentException("thisidUnknownUri:"+uri); } } /** *插入操作 */ @Override publicUriinsert(Uriuri,ContentValuesvalues){ SQLiteDatabasedb=helper.getWritableDatabase(); switch(MATCHER.match(uri)){ case1: longrowid=db.insert("person","name",values);//可以当作是数据表中的主键值 //content://cn.itcast.providers.personprovider/person/10 //Uri //insertUri=Uri.parse("content://cn.itcast.providers.personprovider/person/"+rowid); UriinsertUri=ContentUris.withAppendedId(uri,rowid); returninsertUri; default: thrownewIllegalArgumentException("thisidUnknownUri:"+uri); } } @Override publicbooleanonCreate(){ helper=newDBOpenHelper(this.getContext()); returntrue; } /** *查询操作 */ @Override publicCursorquery(Uriuri,String[]projection,Stringselection, String[]selectionArgs,StringsortOrder){ SQLiteDatabasedb=helper.getWritableDatabase(); switch(MATCHER.match(uri)){ case1: returndb.query("person",projection,selection,selectionArgs, null,null,sortOrder); case2: longrowid=ContentUris.parseId(uri); Stringwhere="personid="+rowid; if(selection!=null&&!"".equals(selection.trim())){ where+="and"+selection; } returndb.query("person",projection,where,selectionArgs,null, null,sortOrder); default: thrownewIllegalArgumentException("thisidUnknownUri:"+uri); } } /** *更新操作 */ @Override publicintupdate(Uriuri,ContentValuesvalues,Stringselection, String[]selectionArgs){ SQLiteDatabasedb=helper.getWritableDatabase(); intnum=0; switch(MATCHER.match(uri)){ case1: db.update("person",values,selection,selectionArgs); break; case2: longrowid=ContentUris.parseId(uri); Stringwhere="personid="+rowid; if(selection!=null&&!"".equals(selection.trim())){ where+="and"+selection; } num=db.update("person",values,where,selectionArgs); break; default: thrownewIllegalArgumentException("thisidUnknownUri:"+uri); } returnnum; } }
开发者的每一个内容提供者都要继承ContentProvider类,然后实现增删改查和onCreate、getType方法。
其中与内容提供者相关的其他常用api简要列举如下:
Uriuri=Uri.parse("content://com.example.providers.personprovider/person"); ContentResolverresolver=this.getContext().getContentResolver(); ContentValuesvalues=newContentValues(); values.put("name","test"); values.put("phone","18688888888"); resolver.insert(uri,values); resolver.delete(uri,null,null); resolver.update(uri,values,null,null); Cursorcursor=resolver.query(uri,null,null,null,"personidasc"); //根据uri获取id //类似于根据content://cn.itcast.providers.personprovider/person/10得到10 longrowid=ContentUris.parseId(uri); //对uri添加id //类似于对content://cn.itcast.providers.personprovider/person得到 content://cn.itcast.providers.personprovider/person/10 UriinsertUri=ContentUris.withAppendedId(uri,rowid);
由于仅仅是记录学习笔记,此实例比较简单,谢谢提出意见。
2.另外在android中数据存储方式主要有以下几种方式:
1)SharedPreferences 一般用于用户喜好参数的存储
2)SQLite数据库 较大型数据 一般存在ExternalStorage SDCard
3) ContentProvider 一般用于对外共享数据
4) 文件 File 类似于java File IO InputStream、OutputStream
5) 网络方式