Android之ContentProvider

晨曦之光 发布于 2012/05/16 17:05
阅读 349
收藏 0

【开源中国 APP 全新上线】“动弹” 回归、集成大模型对话、畅读技术报告”

Android程序的主要4部分:

1Activiyt

2Broadcast Intent Receiver

3Service

4Content Provider

一个ContentProvider类实现了一组标准的方法接口,从而能够让其他的应用保存或读取此ContentProvider的各种数据类型。

下面列举一些常用的接口:

1query(Uri uri,String[] projection,String selection,String[] selectionArgs,String sortOrder):通过Uri进行查询,返回一个Cursor.

2insert(Uri uri,ContentValues values):将一组数据插入到Uri指定的地方。

3update(Uri uri,ContentValues values,String where,String[] selectionArgs):更新Uri指定位置的数据。

4delete(Uri uri,String where,String[] selectionArgs):删除指定Uri并且符合一定条件的数据。

ContentResolver

外界程序通过ContentResolver接口可以访问ContentProvider提供的数据,在Activity当中通过getContentResolver()可以得到当前应用ContentResolver实例。其提供的接口与ContentProvider提供的接口对应:

1、query(Uri uri,String[] projection,String selection,String[] selectionArgs,String sortOrder):通过Uri进行查询,返回一个Cursor.

2、insert(Uri uri,ContentValues values):将一组数据插入到Uri指定的地方。

3、update(Uri uri,ContentValues values,String where,String[] selectionArgs):更新Uri指定位置的数据。

4、delete(Uri uri,String where,String[] selectionArgs):删除指定Uri并且符合一定条件的数据。

实例:

1.要为当前应用程序的私有数据定义URI,就需要专门定义个继承自ContentProvider的类,然后实现各个不同的操作所调用的方法。

1PersonProvider

package cn.class3g.db;

import cn.class3g.service.DatabaseHelper;

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 PersonProvider extends ContentProvider {

private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);

private static final int PERSONS = 1;

private static final int PERSON = 2;

private DatabaseHelper dbHelper;

static {

matcher.addURI("cn.class3g.providers.personprovider", "person", PERSONS);

matcher.addURI("cn.class3g.providers.personprovider", "person/#",

PERSON);

}

public boolean onCreate() {

dbHelper = new DatabaseHelper(this.getContext());

return true;

}

//  content://cn.itcast.provides.personprovider/person

public Uri insert(Uri uri, ContentValues values) {

SQLiteDatabase db = dbHelper.getWritableDatabase();

long rowId;

switch (matcher.match(uri)) {

case PERSONS: //向表中添加新纪录并返回其行号

rowId = db.insert("person", "personid", values);

return ContentUris.withAppendedId(uri, rowId);

default:

throw new IllegalArgumentException("Unknow Uri:" + uri);

}

}

public Cursor query(Uri uri, String[] projection, String selection,

String[] selectionArgs, String sortOrder) {

SQLiteDatabase db = dbHelper.getReadableDatabase();

switch (matcher.match(uri)) {

case PERSONS:

return db.query("person", projection, selection, selectionArgs, null, null, sortOrder);

case PERSON:

long personid = ContentUris.parseId(uri);

String where = "personid="+ personid;

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

where = where + " and "+ selection;

}

return db.query("person", projection, where, selectionArgs, null, null, sortOrder);

default:

throw new IllegalArgumentException("Unknown Uri:"+ uri);

}

}

//  content://cn.itcast.provides.personprovider/person 更新表中的所有记录

//  content://cn.itcast.provides.personprovider/person/10 更新表中指定id的记录

public int update(Uri uri, ContentValues values, String selection,

String[] selectionArgs) {

SQLiteDatabase db = dbHelper.getWritableDatabase();

int num;

switch(matcher.match(uri)){

case PERSONS: //更新指定记录

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

break;

case PERSON:

long personid = ContentUris.parseId(uri);

String where = "personid=" + personid;

if(selection != null){

where += " and " + selection;

}

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

break;

default:

throw new IllegalArgumentException("Unknow Uri"+uri);

}

return num;

}

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

SQLiteDatabase db = dbHelper.getWritableDatabase();

int num = 0;

switch (matcher.match(uri)) {

case PERSONS:

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

break;

case PERSON:

long personid = ContentUris.parseId(uri);

String where = "personid="+ personid;

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

where = where + " and "+ selection;

}

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

break;

default:

throw new IllegalArgumentException("Unknown Uri:"+ uri);

}

return num;

}

public String getType(Uri uri) {

switch (matcher.match(uri)) {

case PERSONS:

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

case PERSON:

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

default:

throw new IllegalArgumentException("Unknown Uri:"+ uri);

}

}

}

2.在AndroidManifest中配置

 <provider

            android:authorities="cn.class3g.providers.personprovider"

            android:name="PersonProvider" >

        </provider>

3.建立测试工程编写测试代码

public class AccessContentProviderTest extends AndroidTestCase {

public void testSave() throws Throwable{

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

Uri insertUri = Uri.parse("content://cn.class3g.providers.personprovider/person");

ContentValues values = new ContentValues();

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

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

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

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

}

public void testQuery() throws Throwable{

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

Uri uri = Uri.parse("content://cn.class3g.providers.personprovider/person");

Cursor cursor = resolver.query(uri, null, null, null, "personid asc");

while(cursor.moveToNext()){

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

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

Log.i("TAG", "personid="+ personid + ",name="+ name);

}

cursor.close();

}

public void testUpdate() throws Throwable{

ContentResolver contentResolver = 

this.getContext().getContentResolver();

Uri updateUri = Uri.parse("content://cn.class3g.providers.personprovider/person/5");

ContentValues values = new ContentValues();

values.put("name", "孙悟空");

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

}

public void testDelete() throws Throwable{

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

Uri uri = Uri.parse("content://cn.class3g.providers.personprovider/person/5");

contentResolver.delete(uri, null, null);

}

}


原文链接: http://blog.csdn.net/xy849288321/article/details/7093353
加载中
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部