初涉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; } @Override public 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"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.hpu.edu" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <provider android:name=".util.PersonProvider" android:authorities="cn.hpu.edu.personprovider"></provider> <activity android:name="cn.hpu.edu.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <uses-library android:name="android.test.runner"/> </application> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="cn.hpu" android:label="Tests for PersonService"></instrumentation> </manifest>
第三步:在另外一个应用里面添加如下测试:
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"))); } } }
相关推荐
从头学Android之ContentProvider示例源代码
Android中ContentProvider的示例,详细可以参见博客:http://www.cnblogs.com/plokmju/p/android_ContentProvider.html
1、Android Room操作SQLite数据 2、ContentProvider使用,使用Room进行数据库操作
android 自定义 ContentProvider 以及 ContentResolver
Android基于ContentProvider的音乐播放器,通过读取系统多媒体信息,得到储存在外部存储器上的所有音频文件内容,用listview显示歌曲名和歌手信息.并通过mediaPlayer进行播放.该版本只是完成了一个音乐播放器的最简单...
Android学习ContentProvider数据更新与Observer模式一Observer模式意图: 定义对象之间一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被更新。 依赖(Dependents)、发布-...
该文件是一个Android程序,是关于ContentProvider应用的例子
NULL 博文链接:https://sunzone.iteye.com/blog/1884167
Android中用ContentProvider快速查找通讯录信息的代码清单.pdf 学习资料 复习资料 教学资源
Android高级编程雪梨作业之自定义ContentProvider 将任务01生词本作业中生成的生词本数据库通过自定义ContentProvider的方式,共享给其他应用。 要求如下: (1) 使用自定义SQLiteOpenHelper来管理数据库; (2) 提交...
本源码主要是演示Android ContentProvider组件的使用,借助一个写日记小程序来演示组件中各种方法的使用技巧,比如向日记本中插入一条数据,编辑当前数据内容、删除当前数据、查询cursor时候,感兴趣的那些条例如何...
用内容提供者操作文件,传输文件,方便与其他应用通信,免的写数据库。
Android知识点ContentProvider篇
android开发contentprovider教学ppt(内部资料).pptx
Android ContentProvider简单实现
Android应用开发使用ContentProvider以及SQLite实现对数据库的相关操作,可以直接操作数据库,也可以使用ContentProvider来对数据库进行间接操作。
android contentprovider使用示例
Android中ContentProvider组件详解示例代码
同我前面的资源一样,我的每个功能的实例都是力图实现最简短话...这个实例不讲求大而全的实现ContentProvider的所有功能,而只是为了便于理解而实现。对应于我的博客“How:ContentProvider基本功能核心框架(增删改查)”
Android基础ContentProvider学习教案.pptx