Android Content provider
来源:互联网 发布:图片展示网站源码 编辑:程序博客网 时间:2024/06/06 20:54
Android Content provider
当应用需要通过Content Provider共享数据时,首先需要继承Content Provider类并重写相应方法。第二步需要在AndroidManifest.xml使用<provider>对Content Provider进行配置。
1. 新建内容提供类:
public class PersonProvider extends ContentProvider { private DBOpenHelper dbHelper; 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("com.example.providers.personprovider", "person", PERSONS); MATCHER.addURI("com.example.providers.personprovider", "person/#", PERSON); } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { SQLiteDatabase db = dbHelper.getWritableDatabase(); int count = 0; switch (MATCHER.match(uri)) { case PERSONS: count = db.delete("person", selection, selectionArgs); return count; case PERSON: long id = ContentUris.parseId(uri); String where = "personid=" + id; if(selection!=null && !"".equals(selection)) { where = selection + " and " + where; } count = db.delete("person", where, selectionArgs); return count; default: throw new IllegalArgumentException("Unknown Uri: " + uri.toString()); } } @Override public String getType(Uri uri) { //返回当前操作的数据的内容类型 switch (MATCHER.match(uri)) { case PERSONS: return "vnd.android.cursor.dir/person"; case PERSON: return "vnd.android.cursor.item/person"; default: throw new IllegalArgumentException("Unknown Uri: " + uri.toString()); } } @Override public Uri insert(Uri uri, ContentValues values) { // TODO Auto-generated method stub SQLiteDatabase db = dbHelper.getWritableDatabase(); switch(MATCHER.match(uri)) { case PERSONS : long rowid = db.insert("person", "null", values); Uri insertUri = ContentUris.withAppendedId(uri, rowid); this.getContext().getContentResolver().notifyChange(uri, null); return insertUri; default: throw new IllegalArgumentException("Unknown Uri: " + uri.toString()); } } @Override public boolean onCreate() { this.dbHelper = new DBOpenHelper(this.getContext()); return false; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteDatabase db = dbHelper.getReadableDatabase(); switch (MATCHER.match(uri)) { case PERSONS: return db.query("person", projection, selection, selectionArgs, null, null, sortOrder); case PERSON: long id = ContentUris.parseId(uri); String where = "personid=" + id; if(selection!=null && !"".equals(selection)) { where = selection + " and " + where; } return db.query("person", projection, where, selectionArgs, null, null, sortOrder); default: throw new IllegalArgumentException("Unknown Uri: " + uri.toString()); } } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { SQLiteDatabase db = dbHelper.getWritableDatabase(); int count = 0; switch (MATCHER.match(uri)) { case PERSONS: count = db.update("person", values, selection, selectionArgs); return count; case PERSON: long id = ContentUris.parseId(uri); String where = "personid=" + id; if(selection!=null && !"".equals(selection)) { where = selection + " and " + where; } count = db.update("person", values, where, selectionArgs); return count; default: throw new IllegalArgumentException("Unknown Uri: " + uri.toString()); } }}
2. 配置Content Provider:
<!--name为Content Provider类名,路径authorities为自定义--><provider android:name=".PersonProvider" android:authorities="com.example.providers.personprovider"/>
3. 新建测试类进行测试:
public class AccessContentProvider extends AndroidTestCase { public void testInsert() throws Exception { ContentResolver contentResolver = this.getContext().getContentResolver(); Uri insertUri = Uri.parse("content://com.example.providers.personprovider/person"); ContentValues values = new ContentValues(); values.put("name", "Jospehus"); values.put("amount", 1000); Uri uri = contentResolver.insert(insertUri, values); Log.i("URI", uri.toString()); } public void testUpdate() throws Exception { ContentResolver contentResolver = this.getContext().getContentResolver(); Uri updateUri = Uri.parse("content://com.example.providers.personprovider/person/8"); ContentValues values = new ContentValues(); values.put("name", "monkey"); contentResolver.update(updateUri, values, null, null); } public void testQuery() throws Exception { ContentResolver contentResolver = this.getContext().getContentResolver(); Uri selectUri = Uri.parse("content://com.example.providers.personprovider/person"); Cursor cursor = contentResolver.query(selectUri, null, null, null, "personid desc"); while(cursor.moveToNext()) { int id = cursor.getInt(cursor.getColumnIndex("personid")); String name = cursor.getString(cursor.getColumnIndex("name")); int amount = cursor.getInt(cursor.getColumnIndex("amount")); Log.i("QUERY", "id=" + id + "name=" + name + "amount=" + amount); } } public void testDelete() throws Exception { ContentResolver contentResolver = this.getContext().getContentResolver(); Uri deleteUri = Uri.parse("content://com.example.providers.personprovider/person/8"); contentResolver.delete(deleteUri, null, null); }}
4. 监听Content Provider中数据的变化
public class OtherActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ContentResolver contentResolver = this.getContentResolver(); Uri insertUri = Uri.parse("content://com.example.providers.personprovider/person"); contentResolver.registerContentObserver( insertUri, true, new PersonObserver(new Handler())); /*ContentValues values = new ContentValues(); values.put("name", "Jospehus"); values.put("amount", 1000); Uri uri = contentResolver.insert(insertUri, values); Log.i("URI", uri.toString());*/ } private final class PersonObserver extends ContentObserver { public PersonObserver(Handler handler) { super(handler); // TODO Auto-generated constructor stub } @Override public void onChange(boolean selfChange) { ContentResolver contentResolver = OtherActivity.this.getContentResolver(); Uri selectUri = Uri.parse("content://com.example.providers.personprovider/person"); Cursor cursor = contentResolver.query(selectUri, null, null, null, "personid desc"); while(cursor.moveToNext()) { int id = cursor.getInt(cursor.getColumnIndex("personid")); String name = cursor.getString(cursor.getColumnIndex("name")); int amount = cursor.getInt(cursor.getColumnIndex("amount")); Log.i("QUERY", "id=" + id + "name=" + name + "amount=" + amount); } } }}
0 0
- Android Content Provider
- Android Content Provider
- Android Content Provider
- Android Content Provider
- Android Content Provider
- Android中的content provider
- android Content Provider
- Android 复习 Content Provider
- Android Content Provider 测试
- android笔记 Content Provider
- Android的Content Provider
- android Content provider 组件
- android content provider 使用
- android Content Provider
- android Content Provider详解
- Android 组件Content Provider
- Android content provider
- Android Content Provider Basics
- 6进程原语:wait()和waitpid()函数
- http长连接与短连接
- 5.2 分布式 Git - 为项目作贡献
- Lombok 安装、入门 - 消除冗长的 java 代码
- 1pipe管道
- Android Content provider
- Java,Mysql-根据一个给定经纬度的点,进行附近500米地点查询–合理利用算法
- lua函数精讲(一)
- 2fifo有名管道
- Qt中添加背景图片的方法
- kafka2.9.2的伪分布式集群安装和demo(java api)测试
- linux学习(一)
- mysql 下 计算 两点 经纬度 之间的距离
- 3Linux内存映射,mmap()函数