阅读更多

初涉Android之ContentProvider

 

 

一、ContentProvider的作用,可以让其它应用访问本应用的数据

第一步:定义ContentProvider如下:

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 helper = null;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.hpu.edu.personprovider", "person", PERSONS);MATCHER.addURI("cn.hpu.edu.personprovider", "person/#", PERSON);}/** * 删除数据库中相应的数据 */public int delete(Uri uri, String selection, String[] selectionArgs) {SQLiteDatabase db = helper.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 = " id = " + rowid;if(selection != null && !"".equals(selection.trim())) {where += " and " + selection;}num = db.delete("person", where, selectionArgs);break;default :throw new IllegalArgumentException("this is An Unkown Uri : " + uri.toString());}return num;}@Overridepublic String getType(Uri uri) {return null;}/** * 向内容提供者里面插入数据 */public Uri insert(Uri uri, ContentValues values) {SQLiteDatabase db = helper.getWritableDatabase();switch(MATCHER.match(uri)) {case PERSONS:long rowid = db.insert("person", "name", values);Uri insertUri = ContentUris.withAppendedId(uri, rowid);return insertUri;default: throw new IllegalArgumentException("this is An Unknown Uri:" + uri.toString());}}/** * 该方法只调用一次用于初始化环境 */public boolean onCreate() {helper = new DBOpenHelper(this.getContext());return true;}/** * 按条件查询结果集并返回游标 */public Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder) {SQLiteDatabase db = helper.getReadableDatabase();switch(MATCHER.match(uri)) {case PERSONS : return db.query("person", projection, selection, selectionArgs, null, null, sortOrder);case PERSON :long rawid = ContentUris.parseId(uri);String where = " id = " + rawid;if(selection != null && !"".equals(selection.trim())) {where += " and " + selection;}return db.query("person", projection, where, selectionArgs, null, null, sortOrder);default :throw new IllegalArgumentException("this is An Unknown Uri : " + uri.toString());}}/** * 更新数据 */public int update(Uri uri, ContentValues values, String selection,String[] selectionArgs) {SQLiteDatabase db = helper.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 = " id = " + rowid;if(selection != null && !"".equals(selection.trim())) {where += " and " + selection;}num = db.update("person", values, where , selectionArgs);break;default :throw new IllegalArgumentException("this is An Unknown Uri : " + uri.toString());}return num;}}

 

第二步:在本应用的AndroidManifest.xml里面声明Provider

 

<?xml version="1.0" encoding="utf-8"?>                                                                                                                

 第三步:在另外一个应用里面添加如下测试:

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 TestAccessContentProvider extends AndroidTestCase {private static final String TAG = "TestAccessContentProvider";private static final String URI = "content://cn.hpu.edu.personprovider/person";public void testInsert() throws Exception {Uri uri = Uri.parse(URI);ContentResolver resolver = this.getContext().getContentResolver();ContentValues values = new ContentValues();values.put("id", 45);values.put("name", "qiernsdf");values.put("phone", "123456789");resolver.insert(uri, values);}public void testDelete() throws Exception {Uri uri = Uri.parse(URI + "/10");ContentResolver resolver = this.getContext().getContentResolver();resolver.delete(uri, null, null);}public void testUpdate() throws Exception {Uri uri = Uri.parse(URI + "/11");ContentResolver resolver = this.getContext().getContentResolver();ContentValues values = new ContentValues();values.put("name" , "updateValues");resolver.update(uri, values, null, null);}public void testFind() throws Exception {Uri uri = Uri.parse(URI);ContentResolver resolver = this.getContext().getContentResolver();Cursor cursor = resolver.query(uri, null, null, null, " id asc");while(cursor.moveToNext()) {Log.i(TAG ,cursor.getString(cursor.getColumnIndex("name")));}}}

 

 

更多相关文章

  1. 【Android】配置文件属性说明
  2. Android(安卓)2.3的camera的虚拟对焦的去除
  3. Android获取 应用程序大小,数据大小,缓存大小
  4. 初涉Android之ContentProvider
  5. Android中安装,启动和卸载应用
  6. Android获取 应用程序大小,数据大小,缓存大小
  7. android 格式化
  8. GoogleAndroid常用的Android常量
  9. Android(安卓)获取通讯录信息

随机推荐

  1. android中如果需要将字符在两行TextView
  2. Android(安卓)IntentService的使用和源码
  3. Android中实现应用切换主题机制
  4. Android客制化------开机拷贝文件到内置
  5. Android(安卓)SQLite 开发教程(2): andro
  6. Android(安卓)学习笔记(3)—— ImageView/R
  7. 免费短信和电话服务TextFree在Android上
  8. Android模拟键盘之菜单键消失
  9. Android学习系列(14)--App调试的几个命令
  10. Android(安卓)EventBus库的使用