ContentProvider 对外共享数据

说明:

一、ContentProvider简介


当应用继承ContentProvider类,并重写该类用于提供数据和存储数据的方法,就可以向其他应用共享其数据。虽然使用其他方法也可以对外共享数据,但数据访问方式会因数据存储的方式而不同,如:采用文件方式对外共享数据,需要进行文件操作读写数据;采用sharedpreferences共享数据,需要使用sharedpreferences API读写数据。而使用ContentProvider共享数据的好处是统一了数据访问方式。
二、Uri类简介
Uri
代表了要操作的数据,Uri主要包含了两部分信息:1.需要操作的ContentProvider2.ContentProvider中的什么数据进行操作,一个Uri由以下几部分组成:

1.scheme
ContentProvider(内容提供者)的scheme已经由Android所规定为:content://
2.
主机名(或Authority):用于唯一标识这个ContentProvider,外部调用者可以根据这个标识来找到它。
3.
路径(path):可以用来表示我们要操作的数据,路径的构建应根据业务而定,如下:
要操作contact表中id10的记录,可以构建这样的路径:/contact/10
要操作contact表中id10的记录的name字段,contact/10/name
要操作contact表中的所有记录,可以构建这样的路径:/contact
要操作的数据不一定来自数据库,也可以是文件等他存储方式,如下:
要操作xml文件中contact节点下的name节点,可以构建这样的路径:/contact/name
如果要把一个字符串转换成Uri,可以使用Uri类中的parse()方法,如下:
Uriuri =Uri.parse("content://com.darren.provider/person")
三、UriMatcherContentUristContentResolver简介
因为Uri代表了要操作的数据,所以我们很经常需要解析Uri,并从Uri中获取数据。Android系统提供了两个用于操作Uri的工具类,分别为UriMatcherContentUris。掌握它们的使用,会便于我们的开发工作。

UriMatcher
:用于匹配Uri,它的用法如下:
1.
首先把你需要匹配Uri路径全部给注册上,如下:
//
常量UriMatcher.NO_MATCH表示不匹配任何路径的返回码(-1)
UriMatcher  uriMatcher= newUriMatcher(UriMatcher.NO_MATCH);
//
如果match()方法匹配content://com.darren.provider/person路径,返回匹配码为1
uriMatcher.addURI(“com.darren.provider”,“person”, 1);//
添加需要匹配uri,如果匹配就会返回匹配码
//
如果match()方法匹配  content://com.darren.provider/person/10路径,返回匹配码为2
uriMatcher.addURI(“com.darren.provider”,“person/#”,2);//#
号为通配符

2.
注册完需要匹配的Uri后,就可以使用uriMatcher.match(uri)方法对输入的Uri进行匹配,如果匹配就返回匹配码,匹配码是调用addURI()方法传入的第三个参数,假设匹配content://com.darren.provider/person路径,返回的匹配码为1

ContentUris
:用于获取Uri路径后面的ID部分,它有两个比较实用的方法:
withAppendedId(uri,id)
用于为路径加上ID部分
parseId(uri)
方法用于从路径中获取ID部分

ContentResolver
:当外部应用需要对ContentProvider中的数据进行添加、删除、修改和查询操作时,可以使用ContentResolver类来完成,要获取ContentResolver对象,可以使用Activity提供的getContentResolver()方法。ContentResolver使用insertdeleteupdatequery方法,来操作数据。


一.新建一个provider类

packagecom.example.db;


importcom.example.service.DBOpenHelper;

importandroid.content.ContentProvider;

importandroid.content.ContentUris;

importandroid.content.ContentValues;

importandroid.content.UriMatcher;

importandroid.database.Cursor;

importandroid.database.sqlite.SQLiteDatabase;

importandroid.net.Uri;


publicclassPersonProvider extendsContentProvider {

privateDBOpenHelper dbOpenHelper;

privatestaticfinalUriMatcher MATCHER= newUriMatcher(UriMatcher.NO_MATCH);

privatestaticfinalintPERSONS= 1;

privatestaticfinalintPERSON= 2;

static{

//authority 就是AndroidManifest.xml文件中

//<providerandroid:name="com.example.contentProvider.PersonProvider"

//android:authorities="com.example.provider"></provider>

//android:authorities="com.example.provider"的值

//MATCHER.addURI(authority, path, code)

MATCHER.addURI("com.darren.provider","person",PERSONS);

MATCHER.addURI("com.darren.provider","person/#",PERSON);

}


/**

*允许外部应用删除数据

*/

@Override

publicintdelete(Uri uri, String selection, String[] selectionArgs) {

SQLiteDatabasedbOperate = dbOpenHelper.getWritableDatabase();

intnum = 0;

//uri进行判断,看是不是合法的uri

switch(MATCHER.match(uri)){

case1:

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

case2:

longrowId = ContentUris.parseId(uri);

String where ="personid = "+ rowId;

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

where =where + "and "+ selection;

}

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

break;

default:

//如果参数错误,不存在这样的Uri就会抛出错误

thrownewIllegalArgumentException("this isUnknown Uri: " + uri);

}

returnnum;

}


/**

*返回目前操作数据的内容类型

*/

@Override

publicString getType(Uri uri) {

switch(MATCHER.match(uri)){

case1:

//如果操作多条数据,就一定要以"vnd.android.cursor.dir"开头

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

case2:

//如果操作一条数据,就一定要以"vnd.android.cursor.item"开头

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

default:

//如果参数错误,不存在这样的Uri就会抛出错误

thrownewIllegalArgumentException("this isUnknown Uri: " + uri);

}

}


/**

*允许外部应用插入数据

*/

@Override

publicUri insert(Uri uri, ContentValues values) {

SQLiteDatabasedbOperate = dbOpenHelper.getWritableDatabase();

//uri进行判断,看是不是合法的uri

switch(MATCHER.match(uri)){

case1:

//如果主键是整型的并且是自增长的,那么行号就等于主键

longrowId = dbOperate.insert("person","name",values);

Uri insertUri =Uri.parse("content://com.darren.provider/person/"+ rowId);

//Uri中追加参数

//ContentUris.withAppendedId(contentUri, id)

//同样的效果

//UriinsertUri = ContentUris.withAppendedId(uri,rowId);

returninsertUri;


default:

//如果参数错误,不存在这样的Uri就会抛出错误

thrownewIllegalArgumentException("this isUnknown Uri: " + uri);

}

}


/**

*当内容提供者被创建以后有系统自动调用只会调用一次

*/

@Override

publicbooleanonCreate() {

dbOpenHelper= newDBOpenHelper(getContext());

returnfalse;

}


@Override

publicCursor query(Uri uri, String[] projection, String selection, String[]selectionArgs,

StringsortOrder) {

Cursor cursor =null;

SQLiteDatabasedbOperate = dbOpenHelper.getReadableDatabase();

//uri进行判断,看是不是合法的uri

switch(MATCHER.match(uri)){

case1:

cursor =dbOperate.query("person",projection, selection, selectionArgs, null,null,sortOrder);

break;

case2:

longrowId = ContentUris.parseId(uri);

String where ="personid = "+ rowId;

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

where =where + "and "+ selection;

}

cursor =dbOperate.query("person",projection, where, selectionArgs, null,null,sortOrder);

break;

default:

//如果参数错误,不存在这样的Uri就会抛出错误

thrownewIllegalArgumentException("this isUnknown Uri: " + uri);

}

returncursor;

}


/**

*允许外部应用更新数据

*/

@Override

publicintupdate(Uri uri, ContentValues values, String selection, String[]selectionArgs) {

SQLiteDatabasedbOperate = dbOpenHelper.getWritableDatabase();

intnum = 0;

//uri进行判断,看是不是合法的uri

switch(MATCHER.match(uri)){

case1:

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

case2:

longrowId = ContentUris.parseId(uri);

String where ="personid = "+ rowId;

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

where =where + "and "+ selection;

}

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

break;

default:

//如果参数错误,不存在这样的Uri就会抛出错误

thrownewIllegalArgumentException("this isUnknown Uri: " + uri);

}

returnnum;

}


}

二.在AndroidManifest.xml文件中添加一行代码

<!--作用:对外共享数据-->

<!--android:authorities 内容提供者的唯一标识值是自定义的-->

<providerandroid:name="com.example.db.PersonProvider"

android:authorities="com.darren.provider"></provider>

这句话放在 < application ></application>之间



二.测试

packagecom.example.test.test;


importandroid.content.ContentResolver;

importandroid.content.ContentValues;

importandroid.database.Cursor;

importandroid.net.Uri;

importandroid.test.AndroidTestCase;

importandroid.util.Log;


publicclassContentProviderTest extendsAndroidTestCase {

privatestaticfinalString TAG= "ContentProviderTest";


publicvoidtestInsert() throwsException {

Uri uri =Uri.parse("content://com.darren.provider/person");

ContentValuesvalues = newContentValues();

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

values.put("phone","23456781");

values.put("amount","400");

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

//这个方法内部会调用内容提供者的insert方法

contentResolver.insert(uri,values);

}


publicvoidtestDelete() throwsException {

Uri uri =Uri.parse("content://com.darren.provider/person/1");

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

contentResolver.delete(uri,null,null);

}


publicvoidtestUpdate() throwsException {

Uri uri =Uri.parse("content://com.darren.provider/person/1");

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

ContentValuesvalues = newContentValues();

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

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

}


publicvoidtestQuery() throwsException {

Uri uri =Uri.parse("content://com.darren.provider/person");

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

Cursor cursor =contentResolver.query(uri, null,null,null,"personid ASC");

while(cursor.moveToNext()) {

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

Log.i(TAG,name);

}

}

}


注:以上代码都是基于以前的对数据库操作的基础上进行 的,可以参看这篇文章以前的关于Android操作的文章


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值