`
isiqi
  • 浏览: 16052809 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

Android之ContentProvider

 
阅读更多

Android程序的主要4部分:

1Activiyt

2BroadcastIntentReceiver

3Service

4ContentProvider

一个ContentProvider类实现了一组标准的方法接口,从而能够让其他的应用保存或读取此ContentProvider的各种数据类型。

下面列举一些常用的接口:

1query(Uriuri,String[]projection,Stringselection,String[]selectionArgs,StringsortOrder):通过Uri进行查询,返回一个Cursor.

2insert(Uriuri,ContentValuesvalues):将一组数据插入到Uri指定的地方。

3update(Uriuri,ContentValuesvalues,Stringwhere,String[]selectionArgs):更新Uri指定位置的数据。

4delete(Uriuri,Stringwhere,String[]selectionArgs):删除指定Uri并且符合一定条件的数据。

ContentResolver

外界程序通过ContentResolver接口可以访问ContentProvider提供的数据,在Activity当中通过getContentResolver()可以得到当前应用ContentResolver实例。其提供的接口与ContentProvider提供的接口对应:

1、query(Uriuri,String[]projection,Stringselection,String[]selectionArgs,StringsortOrder):通过Uri进行查询,返回一个Cursor.

2、insert(Uriuri,ContentValuesvalues):将一组数据插入到Uri指定的地方。

3、update(Uriuri,ContentValuesvalues,Stringwhere,String[]selectionArgs):更新Uri指定位置的数据。

4、delete(Uriuri,Stringwhere,String[]selectionArgs):删除指定Uri并且符合一定条件的数据。

实例:

1.要为当前应用程序的私有数据定义URI,就需要专门定义个继承自ContentProvider的类,然后实现各个不同的操作所调用的方法。

1PersonProvider

packagecn.class3g.db;

importcn.class3g.service.DatabaseHelper;

importandroid.content.ContentProvider;

importandroid.content.ContentUris;

importandroid.content.ContentValues;

importandroid.content.UriMatcher;

importandroid.database.Cursor;

importandroid.database.sqlite.SQLiteDatabase;

importandroid.net.Uri;

publicclassPersonProviderextendsContentProvider{

privatestaticUriMatchermatcher=newUriMatcher(UriMatcher.NO_MATCH);

privatestaticfinalintPERSONS=1;

privatestaticfinalintPERSON=2;

privateDatabaseHelperdbHelper;

static{

matcher.addURI("cn.class3g.providers.personprovider","person",PERSONS);

matcher.addURI("cn.class3g.providers.personprovider","person/#",

PERSON);

}

publicbooleanonCreate(){

dbHelper=newDatabaseHelper(this.getContext());

returntrue;

}

//content://cn.itcast.provides.personprovider/person

publicUriinsert(Uriuri,ContentValuesvalues){

SQLiteDatabasedb=dbHelper.getWritableDatabase();

longrowId;

switch(matcher.match(uri)){

casePERSONS://向表中添加新纪录并返回其行号

rowId=db.insert("person","personid",values);

returnContentUris.withAppendedId(uri,rowId);

default:

thrownewIllegalArgumentException("UnknowUri:"+uri);

}

}

publicCursorquery(Uriuri,String[]projection,Stringselection,

String[]selectionArgs,StringsortOrder){

SQLiteDatabasedb=dbHelper.getReadableDatabase();

switch(matcher.match(uri)){

casePERSONS:

returndb.query("person",projection,selection,selectionArgs,null,null,sortOrder);

casePERSON:

longpersonid=ContentUris.parseId(uri);

Stringwhere="personid="+personid;

if(selection!=null&&!"".equals(selection)){

where=where+"and"+selection;

}

returndb.query("person",projection,where,selectionArgs,null,null,sortOrder);

default:

thrownewIllegalArgumentException("UnknownUri:"+uri);

}

}

//content://cn.itcast.provides.personprovider/person更新表中的所有记录

//content://cn.itcast.provides.personprovider/person/10更新表中指定id的记录

publicintupdate(Uriuri,ContentValuesvalues,Stringselection,

String[]selectionArgs){

SQLiteDatabasedb=dbHelper.getWritableDatabase();

intnum;

switch(matcher.match(uri)){

casePERSONS://更新指定记录

num=db.update("person",values,selection,selectionArgs);

break;

casePERSON:

longpersonid=ContentUris.parseId(uri);

Stringwhere="personid="+personid;

if(selection!=null){

where+="and"+selection;

}

num=db.update("person",values,where,selectionArgs);

break;

default:

thrownewIllegalArgumentException("UnknowUri"+uri);

}

returnnum;

}

publicintdelete(Uriuri,Stringselection,String[]selectionArgs){

SQLiteDatabasedb=dbHelper.getWritableDatabase();

intnum=0;

switch(matcher.match(uri)){

casePERSONS:

num=db.delete("person",selection,selectionArgs);

break;

casePERSON:

longpersonid=ContentUris.parseId(uri);

Stringwhere="personid="+personid;

if(selection!=null&&!"".equals(selection)){

where=where+"and"+selection;

}

num=db.delete("person",where,selectionArgs);

break;

default:

thrownewIllegalArgumentException("UnknownUri:"+uri);

}

returnnum;

}

publicStringgetType(Uriuri){

switch(matcher.match(uri)){

casePERSONS:

return"vnd.android.cursor.dir/person";

casePERSON:

return"vnd.android.cursor.item/person";

default:

thrownewIllegalArgumentException("UnknownUri:"+uri);

}

}

}

2.在AndroidManifest中配置

<provider

android:authorities="cn.class3g.providers.personprovider"

android:name="PersonProvider">

</provider>

3.建立测试工程编写测试代码

publicclassAccessContentProviderTestextendsAndroidTestCase{

publicvoidtestSave()throwsThrowable{

ContentResolverresolver=this.getContext().getContentResolver();

UriinsertUri=Uri.parse("content://cn.class3g.providers.personprovider/person");

ContentValuesvalues=newContentValues();

values.put("name","laozhang");

values.put("age","50");

Uriuri=resolver.insert(insertUri,values);

Log.i("TAG",uri.toString());

}

publicvoidtestQuery()throwsThrowable{

ContentResolverresolver=this.getContext().getContentResolver();

Uriuri=Uri.parse("content://cn.class3g.providers.personprovider/person");

Cursorcursor=resolver.query(uri,null,null,null,"personidasc");

while(cursor.moveToNext()){

intpersonid=cursor.getInt(cursor.getColumnIndex("personid"));

Stringname=cursor.getString(cursor.getColumnIndex("name"));

Log.i("TAG","personid="+personid+",name="+name);

}

cursor.close();

}

publicvoidtestUpdate()throwsThrowable{

ContentResolvercontentResolver=

this.getContext().getContentResolver();

UriupdateUri=Uri.parse("content://cn.class3g.providers.personprovider/person/5");

ContentValuesvalues=newContentValues();

values.put("name","孙悟空");

contentResolver.update(updateUri,values,null,null);

}

publicvoidtestDelete()throwsThrowable{

ContentResolvercontentResolver=this.getContext().getContentResolver();

Uriuri=Uri.parse("content://cn.class3g.providers.personprovider/person/5");

contentResolver.delete(uri,null,null);

}

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics