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。 因为注释解析的比较详细,所以就不过多解释了

/**
002.
* 内容提供者
003.
*
004.
* @author ZhaoKaiQiang
005.
* @time 2014年6月6日
006.
*/
007.
public class StudentProvider extends ContentProvider {
008.
// 数据库操作类,用于获取SQLiteDatabase
009.
private MyDbOpenHelper dbHelper;
010.
 
011.
private static final int STUDENT = 1;
012.
private static final int STUDENTS = 2;
013.
 
014.
// UriMatcher类是一个很重要的类,因为我们需要根据传入的uri,来判断执行相对应的操作
015.
private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
016.
 
017.
// 静态代码块用于初始化MATCHER需要匹配的uri
018.
static {
019.
// MATCHER.addURI(主机名(用于唯一标示一个ContentProvider,这个需要和清单文件中的authorities属性相同),路径(可以用来表示我们要操作的数据,路径的构建应根据业务而定),返回值(用于匹配uri的时候,作为匹配的返回值));
020.
MATCHER.addURI("com.example.mydbdemo.StudentProvider", "student", STUDENTS);
021.
MATCHER.addURI("com.example.mydbdemo.StudentProvider", "student/#", STUDENT);
022.
}
023.
 
024.
// 进行数据的初始化操作
025.
@Override
026.
public boolean onCreate() {
027.
dbHelper = new MyDbOpenHelper(getContext());
028.
return false;
029.
}
030.
 
031.
// 查询
032.
// 如果uri为       content://com.example.mydbdemo.StudentProvider/student
033.
// 则代表查询所有的student表内的数据
034.
// 如果uri为       content://com.example.mydbdemo.StudentProvider/student/6
035.
// 则代表查询student表内id=6的数据
036.
@Override
037.
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
038.
 
039.
SQLiteDatabase db = dbHelper.getReadableDatabase();
040.
//判断传入的uri到底匹配哪一个,从而实现不同的业务需求
041.
switch (MATCHER.match(uri)) {
042.
//查询全部的学生信息
043.
case STUDENTS:
044.
//db.query(表明, 要查询的列(是一个String数组), where条件, where条件中的参数, groupBy, having, sortOrder);
045.
return db.query("student", projection, selection, selectionArgs, null, null, sortOrder);
046.
//查询某一个id对应的学生的信息
047.
case STUDENT:
048.
//取出我们要查询的数据的id
049.
long id = ContentUris.parseId(uri);
050.
String where = "id=" + id;
051.
//将selection查询信息拼接到我们的where条件中
052.
if (selection != null && !"".equals(selection)) {
053.
where = selection + " and " + where;
054.
}
055.
return db.query("student", projection, where, selectionArgs, null, null, sortOrder);
056.
//如uri不匹配,抛出不合法参数的异常
057.
default:
058.
throw new IllegalArgumentException("Unkwon Uri:" + uri.toString());
059.
}
060.
 
061.
}
062.
 
063.
// 插入
064.
@Override
065.
public Uri insert(Uri uri, ContentValues values) {
066.
SQLiteDatabase db = dbHelper.getWritableDatabase();
067.
switch (MATCHER.match(uri)) {
068.
case STUDENTS:
069.
long id = db.insert("student", "name", values);
070.
return ContentUris.withAppendedId(uri, id);
071.
default:
072.
throw new IllegalArgumentException("Uri不匹配");
073.
}
074.
 
075.
}
076.
 
077.
//删除数据
078.
@Override
079.
public int delete(Uri uri, String selection, String[] selectionArgs) {
080.
SQLiteDatabase db = dbHelper.getWritableDatabase();
081.
int count = 0;
082.
switch (MATCHER.match(uri)) {
083.
case STUDENTS:
084.
count = db.delete("student", selection, selectionArgs);
085.
return count;
086.
 
087.
case STUDENT:
088.
long id = ContentUris.parseId(uri);
089.
String where = "id=" + id;
090.
if (selection != null && !"".equals(selection)) {
091.
where = selection + " and " + where;
092.
}
093.
count = db.delete("student", where, selectionArgs);
094.
return count;
095.
 
096.
default:
097.
throw new IllegalArgumentException("Unkwon Uri:" + uri.toString());
098.
}
099.
}
100.
 
101.
//更新数据
102.
@Override
103.
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
104.
SQLiteDatabase db = dbHelper.getWritableDatabase();
105.
int count = 0;
106.
switch (MATCHER.match(uri)) {
107.
case STUDENTS:
108.
count = db.update("student", values, selection, selectionArgs);
109.
return count;
110.
 
111.
case STUDENT:
112.
long id = ContentUris.parseId(uri);
113.
String where = "id=" + id;
114.
if (selection != null && !"".equals(selection)) {
115.
where = selection + " and " + where;
116.
}
117.
count = db.update("student", values, where, selectionArgs);
118.
return count;
119.
 
120.
default:
121.
throw new IllegalArgumentException("Unkwon Uri:" + uri.toString());
122.
}
123.
}
124.
 
125.
// 用于获取MIME Type
126.
@Override
127.
public String getType(Uri uri) {
128.
switch (MATCHER.match(uri)) {
129.
case STUDENT:
130.
return "vnd.android.cursor.item/student";
131.
case STUDENTS:
132.
return "vnd.android.cursor.dir/student";
133.
default:
134.
throw new IllegalArgumentException("Unkwon Uri:" + uri.toString());
135.
}
136.
 
137.
}
138.
 
139.
}

最后,在清单文件中配置expored=true,主机名和name