分享

6. ContentProvider 内容提供者

 黎可图书馆 2013-09-20
一. 定义
ContentProvider在android中的作用是对外共享数据,也就是说你可以通过ContentProvider把应用中的数据共享给其他访问,其他的应用可以通过ContentProvider对你应用中的数据进行增删改查。

二. 实现
这个例子是在数据库操作完成之后的基础上进行的。
(1) 创建StudentProvider类并继承ContentProvider类
①其中UriMatcher用于匹配uri
②MATCHER.addURI("com.matrix.provides.studentprovider", "student/#", STUDENT);中的#号代表数字
③ContentUris中有获得uri中的id以及在uri后添加id的功能。
public class StudentProvider extends ContentProvider {
private final static UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
private DBHelper dbhelper = null;
private static int STUDENT = 1;
private static int STUDENTS = 2;
static{
MATCHER.addURI("com.matrix.provides.studentprovider", "student", STUDENTS);
MATCHER.addURI("com.matrix.provides.studentprovider", "student/#", STUDENT);
}
@Override
public boolean onCreate() {
dbhelper = new DBHelper(getContext());
return true;
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = dbhelper.getWritableDatabase();
int rowNo = 0;
switch (MATCHER.match(uri)) {
case 1:
long studnentId = ContentUris.parseId(uri);
String where = "studentId = "+ studnentId;
if(selection != null && !selection.equals("")){
where += " and " + selection;
}
rowNo = db.delete("student", where, selectionArgs);
break;
case 2:
rowNo = db.delete("student", selection, selectionArgs);
break;

default:
throw new IllegalArgumentException("your uri:"+uri.toString()+" is wrong!");
}
return rowNo;
}

@Override
public String getType(Uri uri) {
String result = "";
switch (MATCHER.match(uri)) {
case 1:
result = "vnd.android.cursor.item/student";
break;
case 2:
result = "vnd.android.cursor.dir/student";
break;
}
return result;
}

@Override
public Uri insert(Uri uri, ContentValues values) {
Uri insertUri = null;
switch (MATCHER.match(uri)) {
case 2:
SQLiteDatabase db = dbhelper.getWritableDatabase();
long studentId = db.insert("student", "name", values);
insertUri = ContentUris.withAppendedId(uri, studentId);
break;
default:
throw new IllegalArgumentException("your uri:"+uri.toString()+" is wrong!");
}
return insertUri;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = dbhelper.getReadableDatabase();
Cursor cursor = null;
switch (MATCHER.match(uri)) {
case 1:
long studnentId = ContentUris.parseId(uri);
String where = "studnetId = "+ studnentId;
if(selection != null && !selection.equals("")){
where += " and " + selection;
}
cursor = db.query("student", projection, where, selectionArgs, null, null, sortOrder);
break;
case 2:
cursor = db.query("student", projection, selection, selectionArgs, null, null, sortOrder);
break;
default:
break;
}
return cursor;
}

@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
SQLiteDatabase db = dbhelper.getWritableDatabase();
int rowNo = 0;
switch (MATCHER.match(uri)) {
case 1:
long studnentId = ContentUris.parseId(uri);
String where = "studentId = "+ studnentId;
if(selection != null && !selection.equals("")){
where += " and " + selection;
}
rowNo = db.update("student", values, where, selectionArgs);
break;
case 2:
rowNo = db.update("student", values, selection, selectionArgs);
break;

default:
throw new IllegalArgumentException("your uri:"+uri.toString()+" is wrong!");
}
return rowNo;
}

}

(2) 在AndroidManifest.xml中对provider进行配置
①android:authorities为这个内容提供者对外访问标示,其他应用将通过这个标志找到相对应的内容提供者。
<provider android:name="com.matrix.androidstudy.StudentProvider" android:authorities="com.matrix.provides.studentprovider"></provider>

(3) 测试
①通过ContentResolver resolver = getContext().getContentResolver();获取resolver
public class Test extends AndroidTestCase {
public void testInsert(){
Uri uri = Uri.parse("content://com.matrix.provides.studentprovider/student");
ContentResolver resolver = getContext().getContentResolver();
ContentValues values = new ContentValues();
values.put("name", "like");
values.put("age", 23);
resolver.insert(uri, values);
}
public void testDelete(){
Uri uri = Uri.parse("content://com.matrix.provides.studentprovider/student/1");
ContentResolver resolver = getContext().getContentResolver();
resolver.delete(uri, null, null);
}
public void testUpdate(){
Uri uri = Uri.parse("content://com.matrix.provides.studentprovider/student/1");
ContentResolver resolver = getContext().getContentResolver();
ContentValues values = new ContentValues();
values.put("name", "good man");
resolver.update(uri, values, null, null);
}
public void testQuery(){
Uri uri = Uri.parse("content://com.matrix.provides.studentprovider/student");
ContentResolver resolver = getContext().getContentResolver();
Cursor cursor = resolver.query(uri, null, null, null, "studentId asc");
while(cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
int id = cursor.getInt(cursor.getColumnIndex("studentId"));
Log.i("student", id+"  "+name);
}
}
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多