本文主要是介绍Android开发入门之采用ContentProvider对外共享数据,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在cn.leigo,db下新建PersonProvider类,该类集成ContentProvider:
package cn.leigo.db;import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;public class PersonProvider extends ContentProvider {@Overridepublic boolean onCreate() {// TODO Auto-generated method stubreturn false;}@Overridepublic Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder) {// TODO Auto-generated method stubreturn null;}@Overridepublic String getType(Uri uri) {// TODO Auto-generated method stubreturn null;}@Overridepublic Uri insert(Uri uri, ContentValues values) {// TODO Auto-generated method stubreturn null;}@Overridepublic int delete(Uri uri, String selection, String[] selectionArgs) {// TODO Auto-generated method stubreturn 0;}@Overridepublic int update(Uri uri, ContentValues values, String selection,String[] selectionArgs) {// TODO Auto-generated method stubreturn 0;}}
并在AndroidManifest,xml文件中进行配置
<provider android:name="cn.leigo.db.PersonProvider" android:authorities="cn.leigo.providers.personprovider"/>
对外提供查询方法:
package cn.leigo.db;import cn.leigo.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;static {MATCHER.addURI("cn.leigo.providers.personprovider", "person", PERSONS);}@Overridepublic boolean onCreate() {dbOpenHelper = new DBOpenHelper(getContext());return true;}@Overridepublic Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder) {return null;}@Overridepublic String getType(Uri uri) {return null;}@Overridepublic Uri insert(Uri uri, ContentValues values) {SQLiteDatabase db = dbOpenHelper.getWritableDatabase();switch (MATCHER.match(uri)) {case PERSONS:long rowid = db.insert("person", "name", values); // 主键值// content://cn.leigo.providers.personprovider/person/10// Uri insertUri =// Uri.parse("content://cn.leigo.providers.personprovider/person/"+// rowid);Uri insertUri = ContentUris.withAppendedId(uri, rowid);return insertUri;default:throw new IllegalArgumentException("this is Unknow Uri:" + uri);}}@Overridepublic int delete(Uri uri, String selection, String[] selectionArgs) {return 0;}@Overridepublic int update(Uri uri, ContentValues values, String selection,String[] selectionArgs) {return 0;}}
然后进行测试,新建一个测试项目other,所要测试的工程为db
package cn.leigo.db.test;import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
import android.test.AndroidTestCase;public class AccessContentProviderTest extends AndroidTestCase {public void testInsert() throws Exception {Uri uri = Uri.parse("content://cn.leigo.providers.personprovider/person");ContentResolver resolver = getContext().getContentResolver();ContentValues values = new ContentValues();values.put("name", "zhaoliu");values.put("phone", "18832358032");values.put("amount", 10000);resolver.insert(uri, values);}
}
可以看到数据已经插入进去了。
对外提供删除方法:
private static final int PERSON = 2;static {MATCHER.addURI("cn.leigo.providers.personprovider", "person", PERSONS);MATCHER.addURI("cn.leigo.providers.personprovider", "person/#", PERSON);}@Overridepublic int delete(Uri uri, String selection, String[] selectionArgs) {SQLiteDatabase db = dbOpenHelper.getWritableDatabase();int num = 0;switch (MATCHER.match(uri)) {case PERSONS:num = db.delete("person", selection, selectionArgs);break;case PERSON:long rowid = ContentUris.parseId(uri);String where = "personid=" + rowid;if (!TextUtils.isEmpty(selection)) {where += " and " + selection;}num = db.delete("person", where, selectionArgs);break;default:throw new IllegalArgumentException("this is Unknow Uri:" + uri);}return num;}
测试类:
public void testDelete() throws Exception {Uri uri = Uri.parse("content://cn.leigo.providers.personprovider/person/20");ContentResolver resolver = getContext().getContentResolver();resolver.delete(uri, null, null);}
运行后:
id为20的数据已删除。
对外提供更新方法:
@Overridepublic int update(Uri uri, ContentValues values, String selection,String[] selectionArgs) {SQLiteDatabase db = dbOpenHelper.getWritableDatabase();int num = 0;switch (MATCHER.match(uri)) {case PERSONS:num = db.update("person", values, selection, selectionArgs);break;case PERSON:long rowid = ContentUris.parseId(uri);String where = "personid=" + rowid;if (!TextUtils.isEmpty(selection)) {where += " and " + selection;}num = db.update("person", values, where, selectionArgs);break;default:throw new IllegalArgumentException("this is Unknow Uri:" + uri);}return num;}
测试类:
public void testUpdate() throws Exception {Uri uri = Uri.parse("content://cn.leigo.providers.personprovider/person/19");ContentResolver resolver = getContext().getContentResolver();ContentValues values = new ContentValues();values.put("name", "wangwu");resolver.update(uri, values, null, null);}
运行后:
id为19的Person name已改为wangwu
对外提供查询方法:
@Overridepublic Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder) {Cursor cursor = null;SQLiteDatabase db = dbOpenHelper.getReadableDatabase();switch (MATCHER.match(uri)) {case PERSONS:cursor = db.query("person", null, selection, selectionArgs, null,null, sortOrder);break;case PERSON:long rowid = ContentUris.parseId(uri);String where = "personid=" + rowid;if (!TextUtils.isEmpty(selection)) {where += " and " + selection;}cursor = db.query("person", projection, where, selectionArgs,null, null, sortOrder);break;default:throw new IllegalArgumentException("this is Unknow Uri:" + uri);}return cursor;}
测试类:
public void testQuery() throws Exception {Uri uri = Uri.parse("content://cn.leigo.providers.personprovider/person");ContentResolver resolver = getContext().getContentResolver();Cursor cursor = resolver.query(uri, null, null, null, null);while (cursor.moveToNext()) {String name = cursor.getString(cursor.getColumnIndex("name"));Log.i(TAG, name);}cursor.close();}
@Overridepublic String getType(Uri uri) {String type = "";switch (MATCHER.match(uri)) {case PERSONS:type = "vnd.android.cursor.dir/person";break;case PERSON:type = "vnd.android.cursor.item/person";break;default:throw new IllegalArgumentException("this is Unknow Uri:" + uri);}return type;}
这篇关于Android开发入门之采用ContentProvider对外共享数据的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!