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

Android四大组件之----内容提供者

2015-04-12 19:02 861 查看
内容提供者说简单了就是像外提供一个URI,然后别人去匹配这个URI就可以使用你定义好的一些方法

下面写个实例来介绍一下ContentProdvider

写一个内容提供者的类

PersonContentProvider 类

package com.example.sqlitedemo.providers;

import com.example.sqlitedemo.db.PersonSQLiteOpenHelper;

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

private static final String AUTHORITY = "com.example.sqlitedemo.providers.PersonContentProvider";
private static final int PRESON_INSERT_CODE = 0;// 操作person表添加的操作的uri匹配码
private static final int PERSON_DELETE_CODE = 1;
private static final int PERSON_UPDATE_CODE = 2;
private static final int PERSON_QUERY_ALL_CODE = 3;
private static final int PERSON_QUERY_ITEM_CODE = 4;

private static UriMatcher uriMatcher;
private static PersonSQLiteOpenHelper mOpenHelper;//数据库帮助类
static{

uriMatcher= new UriMatcher(UriMatcher.NO_MATCH);

//添加分机号
uriMatcher.addURI(AUTHORITY, "person/insert", PRESON_INSERT_CODE);
uriMatcher.addURI(AUTHORITY, "person/delete", PERSON_DELETE_CODE);
uriMatcher.addURI(AUTHORITY, "person/update", PERSON_UPDATE_CODE);
uriMatcher.addURI(AUTHORITY, "person/queryAll", PERSON_QUERY_ALL_CODE);
uriMatcher.addURI(AUTHORITY, "person/query/#", PERSON_QUERY_ITEM_CODE);

}
@Override
public boolean onCreate() {
mOpenHelper = new PersonSQLiteOpenHelper(getContext());
return true;
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
switch (uriMatcher.match(uri)) {
case PERSON_DELETE_CODE:
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
if(db.isOpen()){
int count = db.delete("person", selection, selectionArgs);
db.close();
return count;
}

break;
default:
throw new IllegalArgumentException("uri不匹配: " + uri);
}
return 0;
}

@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)) {
case PERSON_QUERY_ALL_CODE://返回多条MIME-type
return "vnd.android.cursor.dir/person";

case PERSON_QUERY_ITEM_CODE://返回单条MIME-type
return "vnd.android.cursor.item/person";

default:
break;
}

return null;
}

@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
switch (uriMatcher.match(uri)) {
case PRESON_INSERT_CODE:
if(db.isOpen()){
long id = db.insert("person", null, values);
db.close();
return ContentUris.withAppendedId(uri, id);
}
break;

default:
throw new IllegalArgumentException("uri不匹配: " + uri);
}
return null;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = mOpenHelper.getReadableDatabase();

switch (uriMatcher.match(uri)) {
case PERSON_QUERY_ALL_CODE:
if(db.isOpen()){

Cursor cursor = db.query("person", projection, selection, selectionArgs, null, null, sortOrder);
return cursor;
}
break;
case PERSON_QUERY_ITEM_CODE:
if(db.isOpen()){
long id = ContentUris.parseId(uri);
Cursor cursor = db.query("person", projection, "cr_id=?", new String []{String.valueOf(id)}, null, null, sortOrder);
return cursor;

}

break;
default:
throw new IllegalArgumentException("uri不匹配: " + uri);
}

return null;
}

@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
switch (uriMatcher.match(uri)) {
case PERSON_UPDATE_CODE:
SQLiteDatabase db=mOpenHelper.getWritableDatabase();
if(db.isOpen()){
int count = db.update("person", values, selection, selectionArgs);
db.close();
return count;
}

break;
default:
throw new IllegalArgumentException("uri不匹配: " + uri);
}
return 0;
}

}


然后在AndroidManifest.xml的application节点下配置

需要注意一下provider是有权限的

<!-- 自己定义一个权限-->

<permission android:name="aa.bb.cc.read"></permission>
<permission android:name="aa.bb.cc.write"></permission>


<!-- 使用上面定义的权限-->

<provider
android:name=".providers.PersonContentProvider"
android:authorities="com.example.sqlitedemo.providers.PersonContentProvider"
android:readPermission="aa.bb.cc.read"
android:writePermission="aa.bb.cc.write"
></provider>


写完这个之后呢我们将这个应用部署到手机上然后在写一个应用来测试我们写的内容提供者

在新建的应用中写一个测试类test

package com.example.otherContentProdvider;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.test.AndroidTestCase;
import android.util.Log;

public class mytest extends AndroidTestCase {

private static final String TAG = "mytest";

public void testInsert(){
Uri uri = Uri.parse("content://com.example.sqlitedemo.providers.PersonContentProvider/person/insert");
ContentResolver contentResolver = getContext().getContentResolver();
ContentValues values = new ContentValues();
values.put("name", "goudan");
values.put("age", 30);
uri=contentResolver.insert(uri, values);
Log.i(TAG, "uri: " + uri);
long id = ContentUris.parseId(uri);
Log.i(TAG, "添加到::"+id);
}
public void testDetete(){
Uri uri = Uri.parse("content://com.example.sqlitedemo.providers.PersonContentProvider/person/delete");
ContentResolver contentResolver = getContext().getContentResolver();
String where = "cr_id=?";
String []selectionArgs={"2"};
int count = contentResolver.delete(uri, where, selectionArgs);
Log.i(TAG, "删除行:"+count);
}

public void testUpdate(){
Uri uri = Uri.parse("content://com.example.sqlitedemo.providers.PersonContentProvider/person/update");
ContentResolver contentResolver = getContext().getContentResolver();
ContentValues values = new ContentValues();
values.put("name", "Sum");
String where = "cr_id=?";
String []selectionArgs={"6"};
contentResolver.update(uri, values, where, selectionArgs);
}

public void testqueryAll(){
Uri uri = Uri.parse("content://com.example.sqlitedemo.providers.PersonContentProvider/person/queryAll");
ContentResolver contentResolver = getContext().getContentResolver();
String []projection={"cr_id","name","age"};
Cursor cursor=contentResolver.query(uri, projection, null, null, "cr_id desc");
String name;
int id;
int age;
if(cursor!=null &&cursor.getCount()>0){
while(cursor.moveToNext()){
id=cursor.getInt(0);
name=cursor.getString(1);
age=cursor.getInt(2);
Log.i(TAG, "id="+id+",name="+name+",age="+age);
}
}
}
public void testqueryItem(){
Uri uri = Uri.parse("content://com.example.sqlitedemo.providers.PersonContentProvider/person/query/#");
uri=ContentUris.withAppendedId(uri, 1);

ContentResolver contentResolver = getContext().getContentResolver();
Cursor cursor =contentResolver.query(uri, new String[]{"cr_id", "name", "age"}, null, null, "cr_id desc");
if(cursor != null && cursor.moveToFirst()) {
int id = cursor.getInt(0);
String name = cursor.getString(1);
int age = cursor.getInt(2);
cursor.close();
Log.i(TAG, "id: " + id + ", name: " + name + ", age: " + age);
}

}
}
在这个应用的AndroidManifest.xml中也必须加入我们之前定义好的一个权限
<uses-permission android:name="aa.bb.cc.read"/>
<uses-permission android:name="aa.bb.cc.write"/>


测试JUnit test必须要在AndroidManifest中添加配置文件

在manifest节点下添加

<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.example.otherContentProdvider"
>
</instrumentation>


和application节点中

<uses-library android:name="android.test.runner"/>


接着我们就可以执行test中的方法了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: