Android编程使用内容提供者方式(ContentProvider)进行存储的方法
本文实例讲述了Android编程使用内容提供者方式进行存储的方法。分享给大家供大家参考,具体如下:
内容提供者(ContentProvider)主要作用是对外共享数据,如果数据通过内容提供者对外共享了,那么其他应用就可以从内容提供者中查询到数据,并且可更新数据、删除数据、添加数据,如果采用文件的操作模式对外共享数据,数据的访问方式会因为存储方式的不同导致数据的访问方式无法得到统一,不同存储方式文件对外进行共享其访问的ApI是不一样的,如果采用内容提供者对外共享数据就会统一了数据的访问方式。采用统一的API访问共享的数据。
创建内容提供者步骤
1.创建内容提供者需要继承android.content.ContentProvider
2.清单文件中进行配置:
<!--android:name:指定内容提供者的类名,android:authorities:调用内容..名称,随意取 -->
<providerandroid:name=".PersonProvider"android:authorities="cn.test.providers.personprovider"/>
ContentProvider类主要方法
publicbooleanonCreate()
该方法在ContentProvider创建后就会被调用,Android开机后,ContentProvider在其它应用第一次访问它时才会被创建。
publicUriinsert(Uriuri,ContentValuesvalues)
该方法用于供外部应用往ContentProvider添加数据。
publicintdelete(Uriuri,Stringselection,String[]selectionArgs)
该方法用于供外部应用从ContentProvider删除数据。
publicintupdate(Uriuri,ContentValuesvalues,Stringselection,String[]selectionArgs)
该方法用于供外部应用更新ContentProvider中的数据。
publicCursorquery(Uriuri,String[]projection,Stringselection,String[]selectionArgs,StringsortOrder)
该方法用于供外部应用从ContentProvider中获取数据。
示例:
内容提供者类,实现数据的增删改查
publicclassPersonProviderextendsContentProvider{
//创建工具类实现Uri匹配
privatestaticfinalUriMatcherMATCHER=newUriMatcher(UriMatcher.NO_MATCH);
privatestaticfinalintPERSONS=1;
privatestaticfinalintPERSON=2;
static{
MATCHER.addURI("cn.test.providers.personprovider","person",PERSONS);
MATCHER.addURI("cn.test.providers.personprovider","person/#",PERSON);
}
privateDBOpenHelperdbOpenHelper=null;
@Override
publicbooleanonCreate(){
dbOpenHelper=newDBOpenHelper(getContext());
returntrue;
}
@Override
publicCursorquery(Uriuri,String[]projection,Stringselection,
String[]selectionArgs,StringsortOrder){
SQLiteDatabasedb=dbOpenHelper.getReadableDatabase();
switch(MATCHER.match(uri)){
casePERSONS://获取person表中的所有数据/person
returndb.query("person",projection,selection,selectionArgs,null,null,sortOrder);
casePERSON://获取person表中的指定id的数据/person/20
longid=ContentUris.parseId(uri);
Stringwhere="personid="+id;
if(selection!=null&&!"".equals(selection.trim())){
where+="and"+selection;
}
returndb.query("person",projection,where,selectionArgs,null,null,sortOrder);
default:
thrownewIllegalArgumentException("UnknownUri:"+uri);
}
}
@Override
publicStringgetType(Uriuri){
//TODOAuto-generatedmethodstub
returnnull;
}
@Override
publicUriinsert(Uriuri,ContentValuesvalues){
//取得数据库操作实例
SQLiteDatabasedb=dbOpenHelper.getWritableDatabase();
switch(MATCHER.match(uri)){
casePERSONS:
//执行添加,返回行号,如果主健字段是自增长的,那么行号会等于主键id
longrowid=db.insert("person","name",values);
//得到拼好的uri
UriinsertUri=ContentUris.withAppendedId(uri,rowid);
//发出数据变化通知(person表的数据发生变化)
getContext().getContentResolver().notifyChange(uri,null);
returninsertUri;
default:
//不能识别uri
thrownewIllegalArgumentException("UnknownUri:"+uri);
}
}
@Override
publicintdelete(Uriuri,Stringselection,String[]selectionArgs){
SQLiteDatabasedb=dbOpenHelper.getWritableDatabase();
//受影响的行数
intnum=0;
switch(MATCHER.match(uri)){
casePERSONS://删除person表中的所有数据/person
num=db.delete("person",selection,selectionArgs);
break;
casePERSON://删除person表中的指定id的数据/person/20
longid=ContentUris.parseId(uri);
Stringwhere="personid="+id;
if(selection!=null&&!"".equals(selection.trim())){
where+="and"+selection;
}
num=db.delete("person",where,selectionArgs);
break;
default:
thrownewIllegalArgumentException("UnknownUri:"+uri);
}
returnnum;
}
@Override
publicintupdate(Uriuri,ContentValuesvalues,Stringselection,String[]selectionArgs){
SQLiteDatabasedb=dbOpenHelper.getWritableDatabase();
intnum=0;
switch(MATCHER.match(uri)){
casePERSONS://更新person表中的所有数据/person
num=db.update("person",values,selection,selectionArgs);
break;
casePERSON://更新person表中的指定id的数据/person/20
longid=ContentUris.parseId(uri);
Stringwhere="personid="+id;
if(selection!=null&&!"".equals(selection.trim())){
where+="and"+selection;
}
num=db.update("person",values,where,selectionArgs);
break;
default:
thrownewIllegalArgumentException("UnknownUri:"+uri);
}
returnnum;
}
}
其他工程中访问:
publicclassAccessContentProiderTestextendsAndroidTestCase{
publicvoidtestInsert()throwsThrowable{
ContentResolverresolver=getContext().getContentResolver();
Uriuri=Uri.parse("content://cn.test.providers.personprovider/person");
ContentValuesvalues=newContentValues();
values.put("name","lili");
values.put("phone","110");
values.put("amount","3000000000");
resolver.insert(uri,values);
}
publicvoidtestDelete()throwsThrowable{
ContentResolverresolver=getContext().getContentResolver();
Uriuri=Uri.parse("content://cn.test.providers.personprovider/person");
intnum=resolver.delete(uri,null,null);
}
publicvoidtestUpdate()throwsThrowable{
ContentResolverresolver=getContext().getContentResolver();
Uriuri=Uri.parse("content://cn.test.providers.personprovider/person/65");
ContentValuesvalues=newContentValues();
values.put("amount",500);
resolver.update(uri,values,null,null);
}
publicvoidtestQuery()throwsThrowable{
ContentResolverresolver=getContext().getContentResolver();
Uriuri=Uri.parse("content://cn.test.providers.personprovider/person/65");
Cursorcursor=resolver.query(uri,null,null,null,"personidasc");
while(cursor.moveToNext()){
Stringname=cursor.getString(cursor.getColumnIndex("name"));
Log.i("AccessContentProviderTest",name);
}
}
}
希望本文所述对大家Android程序设计有所帮助。