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

Android 使用ContentProvider对外共享数据与监听ContentProvider数据的变化

2013-02-13 23:12 796 查看
package cn.itcast.db;

import cn.itcast.service.DBOpenHelper;
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 DBOpenHelper dbOpenHelper;
private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
private static final int PERSONS = 1;
private static final int PERSON = 2;
static{
MATCHER.addURI("cn.itcast.providers.personprovider", "person", PERSONS);
MATCHER.addURI("cn.itcast.providers.personprovider", "person/#", PERSON);
}
//删除person表中的所有记录   /person
//删除person表中指定id的记录 /person/10
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
int count = 0;
switch (MATCHER.match(uri)) {
case PERSONS:
count = db.delete("person", selection, selectionArgs);
return count;

case PERSON:
long id = ContentUris.parseId(uri);
String where = "personid="+ id;
if(selection!=null && !"".equals(selection)){
where = selection + " and " + where;
}
count = db.delete("person", where, selectionArgs);
return count;

default:
throw new IllegalArgumentException("Unkwon Uri:"+ uri.toString());
}
}

@Override
public String getType(Uri uri) {//返回当前操作的数据的mimeType
switch (MATCHER.match(uri)) {
case PERSONS:
return "vnd.android.cursor.dir/person";

case PERSON:
return "vnd.android.cursor.item/person";

default:
throw new IllegalArgumentException("Unkwon Uri:"+ uri.toString());
}
}

@Override
public Uri insert(Uri uri, ContentValues values) {// /person
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
switch (MATCHER.match(uri)) {
case PERSONS:
long rowid = db.insert("person", "name", values);
Uri insertUri = ContentUris.withAppendedId(uri, rowid);//得到代表新增记录的Uri
this.getContext().getContentResolver().notifyChange(uri, null);
return insertUri;

default:
throw new IllegalArgumentException("Unkwon Uri:"+ uri.toString());
}
}

@Override
public boolean onCreate() {
this.dbOpenHelper = new DBOpenHelper(this.getContext());
return false;
}
//查询person表中的所有记录   /person
//查询person表中指定id的记录 /person/10
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
switch (MATCHER.match(uri)) {
case PERSONS:
return db.query("person", projection, selection, selectionArgs, null, null, sortOrder);

case PERSON:
long id = ContentUris.parseId(uri);
String where = "personid="+ id;
if(selection!=null && !"".equals(selection)){
where = selection + " and " + where;
}
return db.query("person", projection, where, selectionArgs, null, null, sortOrder);

default:
throw new IllegalArgumentException("Unkwon Uri:"+ uri.toString());
}
}

//更新person表中的所有记录   /person
//更新person表中指定id的记录 /person/10
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
int count = 0;
switch (MATCHER.match(uri)) {
case PERSONS:
count = db.update("person", values, selection, selectionArgs);
return count;

case PERSON:
long id = ContentUris.parseId(uri);
String where = "personid="+ id;
if(selection!=null && !"".equals(selection)){
where = selection + " and " + where;
}
count = db.update("person", values, where, selectionArgs);
return count;

default:
throw new IllegalArgumentException("Unkwon Uri:"+ uri.toString());
}
}

}
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.test.AndroidTestCase;
import android.util.Log;
public class AccessContentProviderTest extends AndroidTestCase {
private static final String TAG = "AccessContentProviderTest";

/**
* 往内容提供者添加数据
* @throws Throwable
*/
public void testInsert() throws Throwable{
ContentResolver contentResolver = this.getContext().getContentResolver();
Uri insertUri = Uri.parse("content://cn.itcast.providers.personprovider/person");
ContentValues values = new ContentValues();
values.put("name", "zhangxiaoxiao");
values.put("amount", 90);
Uri uri = contentResolver.insert(insertUri, values);
Log.i(TAG, uri.toString());
}
/**
* 更新内容提供者中的数据
* @throws Throwable
*/
public void testUpdate() throws Throwable{
ContentResolver contentResolver = this.getContext().getContentResolver();
Uri updateUri = Uri.parse("content://cn.itcast.providers.personprovider/person/1");
ContentValues values = new ContentValues();
values.put("name", "lili");
contentResolver.update(updateUri, values, null, null);
}

/**
* 从内容提供者中删除数据
* @throws Throwable
*/
public void testDelete() throws Throwable{
ContentResolver contentResolver = this.getContext().getContentResolver();
Uri deleteUri = Uri.parse("content://cn.itcast.providers.personprovider/person/1");
contentResolver.delete(deleteUri, null, null);
}

/**
* 获取内容提供者中的数据
* @throws Throwable
*/
public void testFind() throws Throwable{
ContentResolver contentResolver = this.getContext().getContentResolver();
Uri selectUri = Uri.parse("content://cn.itcast.providers.personprovider/person");
Cursor cursor = contentResolver.query(selectUri, null, null, null, "personid desc");
while(cursor.moveToNext()){
int id = cursor.getInt(cursor.getColumnIndex("personid"));
String name = cursor.getString(cursor.getColumnIndex("name"));
int amount = cursor.getInt(cursor.getColumnIndex("amount"));
Log.i(TAG, "id="+ id + ",name="+ name+ ",amount="+ amount);
}
}
}
package cn.itcast.other;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

public class OtherActivity extends Activity {
private static final String TAG = "OtherActivity";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Uri insertUri = Uri.parse("content://cn.itcast.providers.personprovider/person");
ContentResolver contentResolver = this.getContentResolver();
//对指定uri进行监听,如果该uri代表的数据发生变化,就会调用PersonObserver中的onChange()
contentResolver.registerContentObserver(insertUri, true, new PersonObserver(new Handler()));
}

private final class PersonObserver extends ContentObserver{
public PersonObserver(Handler handler) {
super(handler);
}

@Override
public void onChange(boolean selfChange) {
ContentResolver contentResolver = getContentResolver();
Uri selectUri = Uri.parse("content://cn.itcast.providers.personprovider/person");
Cursor cursor = contentResolver.query(selectUri, null, null, null, "personid desc");
while(cursor.moveToNext()){
int id = cursor.getInt(cursor.getColumnIndex("personid"));
String name = cursor.getString(cursor.getColumnIndex("name"));
int amount = cursor.getInt(cursor.getColumnIndex("amount"));
Log.i(TAG, "id="+ id + ",name="+ name+ ",amount="+ amount);
}
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐