android内容提供者作用,Android内容提供者

1.什么是ContentProvider

首先,ContentProvider(内容提供者)是android中的四大组件之一,但是在一般的开发中,可能使用的比较少。 ContentProvider为不同的软件之间数据共享,提供统一的接口。也就是说,如果我们想让其他的应用使用我们自己程序内的数据,就可以使用ContentProvider定义一个对外开放的接口,从而使得其他的应用可以使用咱们应用的文件、数据库内存储的信息。当然,自己开发的应用需要给其他应用共享信息的需求可能比较少见,但是在Android系统中,很多系统自带应用,比如联系人信息,图片库,音频库等应用,为了对其他应用暴露数据,所以就使用了ContentProvider机制。所以,我们还是要学习ContentProvider的基本使用,在遇到获取联系人信息,图片库,音频库等需求的时候,才能更好的实现功能

2.如何定义一个ContentProvider

Android系统为了让我们更好的对外暴露数据,提供了统一的接口,所以定义了抽象类ContentProvider,因此,如果我们想对外提供数据,我们需要继承ContentProvider,并且实现下面的这几个方法:

onCreate() 当我们的provider初始化时被调用,我们应该在这个方法里面完成部分初始化操作 query() 查询方法,用于给调用者返回数据 insert() 插入操作,用于让外部应用插入数据到内容提供者中 update() 更新操作,用于更新内容提供者的数据 delete() 用于删除数据 getType 返回内容提供者的MIME Type

上面这些方法,当我们继承自ContentProvider的时候,eclipse会自动的给我们添加,但是这并不代表我们每个方法都需要自定义实现。如果我们只希望给其他应用提供数据,而不允许其他应用修改我们的数据,那么我们只需要实现onCreate(),getType()和query()这三个方法就可以了,其他的三个方法我们可以根据业务需求,实现或者是不实现。

因为一般使用ContentProvider向外部暴露数据库的信息,因此,本篇将以使用ContentProvider向其他应用暴露数据库信息为例,讲解ContentProvider的基本使用。

Android中SQLite数据库的创建和使用,本篇不再介绍,不清楚的请看这篇文章 SQLite数据库的简单实用

假设读者已经学会了SQLite数据库的使用,并且已经建立好了数据库,下面我们开始写我们的ContentProvider。 因为注释解析的比较详细,所以就不过多解释了

/**

* Copyright  :版权所有2016

* Author    :坏叔叔

* Date      : 2016/8/21 16:51

*/

public classStudentProviderextendsContentProvider {

//数据库操作类,用于获取SQLiteDatabase

privateMyDbOpenHelperdbHelper;

private static final intSTUDENT=1;

private static final intSTUDENTS=2;

// UriMatcher类是一个很重要的类,因为我们需要根据传入的uri,来判断执行相对应的操作

private static finalUriMatcherMATCHER=newUriMatcher(UriMatcher.NO_MATCH);

//静态代码块用于初始化MATCHER需要匹配的uri

static{

// MATCHER.addURI(主机名(用于唯一标示一个ContentProvider,这个需要和清单文件中的authorities属性相同),路径(可以用来表示我们要操作的数据,路径的构建应根据业务而定),返回值(用于匹配uri的时候,作为匹配的返回值));

MATCHER.addURI("com.example.mydbdemo.StudentProvider","student",STUDENTS);

MATCHER.addURI("com.example.mydbdemo.StudentProvider","student/#",STUDENT);

}

@Override

public booleanonCreate() {

dbHelper=newMyDbOpenHelper(getContext());

return false;

}

//查询

//如果uri为content://com.example.mydbdemo.StudentProvider/student

//则代表查询所有的student表内的数据

//如果uri为content://com.example.mydbdemo.StudentProvider/student/6

//则代表查询student表内id=6的数据

@Override

publicCursorquery(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

SQLiteDatabase db =dbHelper.getReadableDatabase();

//判断传入的uri到底匹配哪一个,从而实现不同的业务需求

switch(MATCHER.match(uri)) {

//查询全部的学生信息

caseSTUDENTS:

//db.query(表明,要查询的列(是一个String数组), where条件, where条件中的参数, groupBy, having, sortOrder);

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

//查询某一个id对应的学生的信息

caseSTUDENT:

//取出我们要查询的数据的id

longid = ContentUris.parseId(uri);

String where ="id="+ id;

//将selection查询信息拼接到我们的where条件中

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

where = selection +" and "+ where;

}

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

//如uri不匹配,抛出不合法参数的异常

default:

throw newIllegalArgumentException("Unkwon Uri:"+ uri.toString());

}

}

//获取MIME TYPE

@Override

publicStringgetType(Uri uri) {

switch(MATCHER.match(uri)) {

caseSTUDENT:

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

caseSTUDENTS:

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

default:

throw newIllegalArgumentException("Unkwon Uri:"+ uri.toString());

}

}

//插入数据

@Override

publicUriinsert(Uri uri, ContentValues values) {

SQLiteDatabase db =dbHelper.getWritableDatabase();

switch(MATCHER.match(uri)) {

caseSTUDENTS:

longid = db.insert("student","name", values);

returnContentUris.withAppendedId(uri, id);

default:

throw newIllegalArgumentException("Uri不匹配");

}

}

//删除数据

@Override

public intdelete(Uri uri, String selection, String[] selectionArgs) {

SQLiteDatabase db =dbHelper.getWritableDatabase();

intcount =0;

switch(MATCHER.match(uri)) {

caseSTUDENTS:

count = db.delete("student", selection, selectionArgs);

returncount;

caseSTUDENT:

longid = ContentUris.parseId(uri);

String where ="id="+ id;

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

where = selection +" and "+ where;

}

count = db.delete("student", where, selectionArgs);

returncount;

default:

throw newIllegalArgumentException("Unkwon Uri:"+ uri.toString());

}

}

//更新数据

@Override

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

SQLiteDatabase db =dbHelper.getWritableDatabase();

intcount =0;

switch(MATCHER.match(uri)) {

caseSTUDENTS:

count = db.update("student", values, selection, selectionArgs);

returncount;

caseSTUDENT:

longid = ContentUris.parseId(uri);

String where ="id="+ id;

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

where = selection +" and "+ where;

}

count = db.update("student", values, where, selectionArgs);

returncount;

default:

throw newIllegalArgumentException("Unkwon Uri:"+ uri.toString());

}

}

}

我们在定义好我们的ContentProvider之后,因为ContentProvider数据四大组件之一,因此我们还需要在AndroidManifest清单文件中进行注册才能使用,下面是注册信息

android:name="com.example.mydbdemo.StudentProvider"

android:exported="true"

android:authorities="com.example.mydbdemo.StudentProvider" >

注意,provider的声明和activity一样,都是在application节点进行声明的。

至此,我们就完成了我们自己的ContentProvider的生命,其他的应用现在就可以使用我们往外部暴露的数据信息了。

3.外部应用如何使用我们的ContentProvider

我们已经定义好了我们自己的ContentProvider,那么外部应用如何调用呢? 下面,我将新建一个测试单元工程,完成对ContentProvider的各个方法的测试

添加方法测试

//使用ContentProvider添加数据的测试

public void testadd() throws Throwable {

//获取ContentResolver对象,完成对ContentProvider的调用

ContentResolver contentResolver = this.getContext().getContentResolver();

//构建我们的uir,这个uri

Uri insertUri = Uri.parse("content://com.example.mydbdemo.StudentProvider/student");

ContentValues values = new ContentValues();

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

values.put("age", 91);

values.put("school", "bbbb");

//返回值为我们刚插入进入的数据的uri地址

Uri uri = contentResolver.insert(insertUri, values);

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

}

删除测试方法

//使用ContentProvider删除数据的测试

public void testDelete() throws Throwable {

ContentResolver contentResolver = this.getContext().getContentResolver();

//删除id为6的学生信息

Uri deleteUri = Uri.parse("content://com.example.mydbdemo.StudentProvider/student/6");

contentResolver.delete(deleteUri, null, null);

}

修改测试方法

//使用ContentProvider更新数据的测试

public void testUpdate() throws Throwable {

ContentResolver contentResolver = this.getContext().getContentResolver();

//更新id = 6 的学生信息

Uri updateUri = Uri.parse("content://com.example.mydbdemo.StudentProvider/student/6");

ContentValues values = new ContentValues();

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

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

values.put("school", "ccccc");

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

}

查询数据测试

//使用ContentProvider查询数据的测试

public void testFind() throws Throwable {

ContentResolver contentResolver = this.getContext().getContentResolver();

//这个uri用于查询所有的数据,若查询某个id的数据,则构建下面的uri

//Uri selectUri = Uri.parse("content://com.example.mydbdemo.StudentProvider/student/要查询的id");

Uri selectUri = Uri.parse("content://com.example.mydbdemo.StudentProvider/student");

Cursor cursor = contentResolver.query(selectUri, null, null, null, "id desc");

while (cursor.moveToNext()) {

int id = cursor.getInt(cursor.getColumnIndex("id"));

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

int age = cursor.getInt(cursor.getColumnIndex("age"));

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

Log.i(TAG, "id=" + id + ",name=" + name + ",age=" + age +",school="+school);

}

}

上面的方法都经过了单元测试。

好了,至此,我们就使用ContentProvider实现了在第三方应用中对我们应用的数据库进行增删改查等操作

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值