您的位置:首页 > 移动开发 > Android开发

Android(四)数据存储之四ContentProvider

2010-04-16 12:50 477 查看
早上我们简要的对SQLite进行回顾,然后将SQLite的事务管理和SQLiteDataBase提供的Insert、Update、Delete、Query方法进行了简单的讲解。
今日的重点内容是ContentProvider(内容提供者)和网络存储,我将对这两大内容进行总结。关于SQLiteDataBase提供的便捷方法,它们会在ContentProvider被使用。SQLite的事务管理比较简单,昨天有简要介绍,今日就不做总结了。
ContentProvider和网络存储将分为两篇日志,本篇总结ContentProvider。
一、ContentProvider简介
当应用继承ContentProvider类,并重写该类用于提供数据和存储数据的方法,就可以向其他应用共享其数据。虽然使用其他方法也可以对外共享数据,但数据访问方式会因数据存储的方式而不同,如:采用文件方式对外共享数据,需要进行文件操作读写数据;采用sharedpreferences共享数据,需要使用sharedpreferences API读写数据。而使用ContentProvider共享数据的好处是统一了数据访问方式。
二、Uri类简介
Uri代表了要操作的数据,Uri主要包含了两部分信息:1.需要操作的ContentProvider ,2.对ContentProvider中的什么数据进行操作,一个Uri由以下几部分组成:



1.scheme:ContentProvider(内容提供者)的scheme已经由Android所规定为:content://。
2.主机名(或Authority):用于唯一标识这个ContentProvider,外部调用者可以根据这个标识来找到它。
3.路径(path):可以用来表示我们要操作的数据,路径的构建应根据业务而定,如下:
· 要操作contact表中id为10的记录,可以构建这样的路径:/contact/10
· 要操作contact表中id为10的记录的name字段, contact/10/name
· 要操作contact表中的所有记录,可以构建这样的路径:/contact
要操作的数据不一定来自数据库,也可以是文件等他存储方式,如下:
要操作xml文件中contact节点下的name节点,可以构建这样的路径:/contact/name
如果要把一个字符串转换成Uri,可以使用Uri类中的parse()方法,如下:
Uri uri = Uri.parse("content://com.changcheng.provider.contactprovider/contact")
三、UriMatcher、ContentUrist和ContentResolver简介
因为Uri代表了要操作的数据,所以我们很经常需要解析Uri,并从Uri中获取数据。Android系统提供了两个用于操作Uri的工具类,分别为UriMatcher 和ContentUris 。掌握它们的使用,会便于我们的开发工作。
UriMatcher用于匹配Uri,它的用法如下:
1.首先把你需要匹配Uri路径全部给注册上,如下:
//常量UriMatcher.NO_MATCH表示不匹配任何路径的返回码(-1)。
UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
//如果match()方法匹配content://com.changcheng.sqlite.provider.contactprovider/contact路径,返回匹配码为1
uriMatcher.addURI(“com.changcheng.sqlite.provider.contactprovider”, “contact”, 1);//添加需要匹配uri,如果匹配就会返回匹配码
//如果match()方法匹配 content://com.changcheng.sqlite.provider.contactprovider/contact/230路径,返回匹配码为2
uriMatcher.addURI(“com.changcheng.sqlite.provider.contactprovider”, “contact/#”, 2);//#号为通配符
2.注册完需要匹配的Uri后,就可以使用uriMatcher.match(uri)方法对输入的Uri进行匹配,如果匹配就返回匹配码,匹配码是调用addURI()方法传入的第三个参数,假设匹配content://com.changcheng.sqlite.provider.contactprovider/contact路径,返回的匹配码为1。
ContentUris用于获取Uri路径后面的ID部分,它有两个比较实用的方法:
· withAppendedId(uri, id)用于为路径加上ID部分
· parseId(uri)方法用于从路径中获取ID部分
ContentResolver当外部应用需要对ContentProvider中的数据进行添加、删除、修改和查询操作时,可以使用ContentResolver 类来完成,要获取ContentResolver 对象,可以使用Activity提供的getContentResolver()方法。 ContentResolver使用insert、delete、update、query方法,来操作数据。
四、ContentProvider示例程序
我们为昨天的SQLite示例程序添加一个ContentProvider,供其他应用来访问我们的数据。
1.为SQLite示例程序添加ContentProvider类
package com.changcheng.sqlite.provider;
import com.changcheng.sqlite.MyOpenHelper;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
public class ContactContentProvider extends ContentProvider {
// 通过UriMatcher匹配外部请求
private static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
// 通过openHelper进行数据库读写
private MyOpenHelper openHelper;
// 匹配状态常量
private static final int CONTACT_LIST = 1;
private static final int CONTACT = 2;
// 表名
private static final String tableName = "contacts";
// 添加Uri
static {
uriMatcher.addURI("com.changcheng.sqlite.provider", "contact",
CONTACT_LIST);
uriMatcher.addURI("com.changcheng.sqlite.provider", "contact/#",
CONTACT);
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = this.openHelper.getWritableDatabase();
int result;
switch (uriMatcher.match(uri)) {
case CONTACT_LIST:
result = db.delete(tableName, selection, selectionArgs);
break;
case CONTACT:
long id = ContentUris.parseId(uri);
String where = "_id=" + id;
if (selection != null && !"".equals(selection)) {
where = where + " and " + selection;
}
result = db.delete(tableName, where, selectionArgs);
break;
default:
throw new IllegalArgumentException("Uri IllegalArgument:" + uri);
}
return result;
}
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)) {
case CONTACT_LIST:// 集合类型必须在前面加上vnd.android.cursor.dir/
return "vnd.android.cursor.dir/contactlist";
case CONTACT:// 非集合类型必须在前面加上vnd.android.cursor.item/
return "vnd.android.cursor.item/contact";
default:
throw new IllegalArgumentException("Uri IllegalArgument:" + uri);
}
}
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = this.openHelper.getWritableDatabase();
long id;
switch (uriMatcher.match(uri)) {
case CONTACT_LIST:
// 因为后台需要生成SQL语句,当values为null时,必须提第二个参数。生成的SQL语句才不会出错!
id = db.insert(tableName, "_id", values);
return ContentUris.withAppendedId(uri, id);
case CONTACT:
id = db.insert(tableName, "_id", values);
String uriPath = uri.toString();
String path = uriPath.substring(0, uriPath.lastIndexOf("/")) + id;
return Uri.parse(path);
default:
throw new IllegalArgumentException("Uri IllegalArgument:" + uri);
}
}
@Override
public boolean onCreate() {
this.openHelper = new MyOpenHelper(this.getContext());
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = this.openHelper.getWritableDatabase();
switch (uriMatcher.match(uri)) {
case CONTACT_LIST:
return db.query(tableName, projection, selection, selectionArgs,
null, null, sortOrder);
case CONTACT:
long id = ContentUris.parseId(uri);
String where = "_id=" + id;
if (selection != null && !"".equals(selection)) {
where = where + " and " + selection;
}
return db.query(tableName, projection, where, selectionArgs, null,
null, sortOrder);
default:
throw new IllegalArgumentException("Uri IllegalArgument:" + uri);
}
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
SQLiteDatabase db = this.openHelper.getWritableDatabase();
int result;
switch (uriMatcher.match(uri)) {
case CONTACT_LIST:
result = db.update(selection, values, selection, selectionArgs);
break;
case CONTACT:
long id = ContentUris.parseId(uri);
String where = "_id=" + id;
if (selection != null && !"".equals(selection)) {
where = where + " and " + selection;
}
result = db.update(tableName, values, where, selectionArgs);
break;
default:
throw new IllegalArgumentException("Uri IllegalArgument:" + uri);
}
return result;
}
}
2.添加ContentProvider配置
<provider android:name=".provider.ContactContentProvider" android:authorities="com.changcheng.sqlite.provider.contactprovider"/>
3.测试SQLite示例程序的ContentProvider
ContentProvider即然是提供给其他应用访问本应用数据的,所以我们需要另创建一个Android应用,来测试SQLite示例程序的ContentProvider。我在此只列出query的测试方法testQuery:
public void testQuery() throws Throwable {
ContentResolver contentResolver = this.getContext()
.getContentResolver();
Uri uri = Uri
.parse("content://com.changcheng.sqlite.provider/contact");
Cursor cursor = contentResolver.query(uri, new String[] { "_id",
"name", "phone" }, null, null, "_id desc");
while (cursor.moveToNext()) {
Log.i(TAG, "_id=" + cursor.getInt(0) + ",name="
+ cursor.getString(1) + ",phone=" + cursor.getString(2));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: