摘要: 本文主要记录了android四大组件之ContentProvider的作用,用法和主要api 等,内容比较简单,仅仅做于学习内容的回顾和温习之用,不妥之处敬请指出。
1.ContentProvider主要用于对外共享数据。其他应用可以通过uri进行数据的访问甚至是增删改查等操作。
以下小例进行简要说明
public class PersonProvider extends ContentProvider{
//DBOpenHelper 工具类,继承于SQLiteOpenHelper 此文省略代码
private DBOpenHelper helper;
private static final UriMatcher MATCHER = new UriMatcher(
UriMatcher.NO_MATCH);
private static final int PERSONS = 1;
private static final int PERSON = 2;
/**
* 在AndroidManifest.xml中注册provider
* <provider android:name="com.example.sqlite.PersonProvider"
* android:authorities="com.example.providers.personprovider"/>
*/
static {
MATCHER.addURI("com.example.providers.personprovider", "person", PERSONS);
MATCHER.addURI("com.example.providers.personprovider", "person/#", PERSON);
}
/**
* 删除操作
*/
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = helper.getWritableDatabase();
int num = 0;
switch (MATCHER.match(uri)) {
case 1:
db.delete("person", selection, selectionArgs);
break;
case 2:
long rowid = ContentUris.parseId(uri);
String where = "personid=" + rowid;
if (selection != null && !"".equals(selection.trim())) {
where += " and " + selection;
}
num = db.delete("person", where, selectionArgs);
break;
default:
throw new IllegalArgumentException("this id Unknown Uri:" + uri);
}
return num;
}
/**
* 该方法返回当前uri所代表数据的MIME类型,
* 若操作的数据属于集合类型,那么MIME类型字符串应该以vnd.android.cursor.dir开头
* 若操作的数据属于非集合类型,那么MIME类型字符串应该以vnd.android.cursor.item开头
*/
@Override
public String getType(Uri uri) {
switch (MATCHER.match(uri)) {
case 1:
return "vnd.android.cursor.dir/person";
case 2:
return "vnd.android.cursor.item/person";
default:
throw new IllegalArgumentException("this id Unknown Uri:" + uri);
}
}
/**
* 插入操作
*/
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = helper.getWritableDatabase();
switch (MATCHER.match(uri)) {
case 1:
long rowid = db.insert("person", "name", values);// 可以当作是数据表中的主键值
// content://cn.itcast.providers.personprovider/person/10
// Uri
// insertUri=Uri.parse("content://cn.itcast.providers.personprovider/person/"+rowid);
Uri insertUri = ContentUris.withAppendedId(uri, rowid);
return insertUri;
default:
throw new IllegalArgumentException("this id Unknown Uri:" + uri);
}
}
@Override
public boolean onCreate() {
helper = new DBOpenHelper(this.getContext());
return true;
}
/**
* 查询操作
*/
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = helper.getWritableDatabase();
switch (MATCHER.match(uri)) {
case 1:
return db.query("person", projection, selection, selectionArgs,
null, null, sortOrder);
case 2:
long rowid = ContentUris.parseId(uri);
String where = "personid=" + rowid;
if (selection != null && !"".equals(selection.trim())) {
where += " and " + selection;
}
return db.query("person", projection, where, selectionArgs, null,
null, sortOrder);
default:
throw new IllegalArgumentException("this id Unknown Uri:" + uri);
}
}
/**
* 更新操作
*/
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
SQLiteDatabase db = helper.getWritableDatabase();
int num = 0;
switch (MATCHER.match(uri)) {
case 1:
db.update("person", values, selection, selectionArgs);
break;
case 2:
long rowid = ContentUris.parseId(uri);
String where = "personid=" + rowid;
if (selection != null && !"".equals(selection.trim())) {
where += " and " + selection;
}
num = db.update("person", values, where, selectionArgs);
break;
default:
throw new IllegalArgumentException("this id Unknown Uri:" + uri);
}
return num;
}
}
开发者的每一个内容提供者都要继承ContentProvider类,然后实现增删改查和onCreate、getType方法。
其中与内容提供者相关的其他常用api简要列举如下:
Uri uri = Uri.parse("content://com.example.providers.personprovider/person");
ContentResolver resolver = this.getContext().getContentResolver();
ContentValues values = new ContentValues();
values.put("name", "test");
values.put("phone", "18688888888");
resolver.insert(uri, values);
resolver.delete(uri, null, null);
resolver.update(uri, values, null, null);
Cursor cursor=resolver.query(uri, null, null, null, "personid asc");
//根据uri获取id
//类似于根据content://cn.itcast.providers.personprovider/person/10得到10
long rowid = ContentUris.parseId(uri);
//对uri添加id
//类似于对content://cn.itcast.providers.personprovider/person得到
content://cn.itcast.providers.personprovider/person/10
Uri insertUri = ContentUris.withAppendedId(uri, rowid);
由于仅仅是记录学习笔记,此实例比较简单,谢谢提出意见。
2.另外在android中数据存储方式主要有以下几种方式:
1)SharedPreferences 一般用于用户喜好参数的存储
2)SQLite数据库 较大型数据 一般存在ExternalStorage SDCard
3) ContentProvider 一般用于对外共享数据
4) 文件 File 类似于java File IO InputStream、OutputStream
5) 网络方式