ContentProvider的一个例子
转自:http://book.51cto.com/art/200908/142708.htm
9.3 ContentProvider
当数据需要在应用程序间共享时,我们就可以利用ContentProvider为数据定义一个URI。之后其他应用程序对数据进行查询或者修改时,只需要从当前上下文对象获得一个ContentResolver(内容解析器)传入相应的URI就可以了。本节中将以前面创建的code.db数据库为例,向读者介绍如何定义一个ContentProvider,以及如何在其他程序中使用ContentResolver访问URI所指定的数据。
9.3.1 定义ContentProvider(1)
要为当前应用程序的私有数据定义URI,就需要专门定义一个继承自ContentProvider的类,然后根据不同的操作调用的方法去实现这些方法的功能。下面我们用SQLite2这个例子,来为它的数据库code.db定义一个URI。
首先,在SQLite2的包中创建一个新类ContryCode.java,来装入所有与数据库操作有关的静态字段,以便于打包成JAR文件供其他应用程序调用。
1 package com.studio.android.chp9.ex3; 2 3 import android.net.Uri; 4 5 public class CountryCode { 6 7 public static final String DB_NAME = "code.db"; 8 public static final String TB_NAME = "countrycode"; 9 public static final int VERSION = 1; 10 11 public static final String ID = "_id"; 12 public static final String COUNTRY = "country"; 13 public static final String CODE = "code"; 14 15 public static final String AUTHORITY = 16 "com.studio.andriod.provider.countrycode"; 17 public static final int ITEM = 1; 18 public static final int ITEM_ID = 2; 19 20 public static final String CONTENT_TYPE = 21 "vnd.android.cursor.dir/vnd.studio.android.countrycode"; 22 public static final String CONTENT_ITEM_TYPE = 23 "vnd.android.cursor.item/vnd.studio.android.countrycode"; 24 25 public static final Uri CONTENT_URI = 26 Uri.parse("content://" + AUTHORITY + "/item"); 27 } public class MyProvider extends ContentProvider { MyHelper dbHelper; private static final UriMatcher sMatcher; static { sMatcher = new UriMatcher(UriMatcher.NO_MATCH); sMatcher.addURI(CountryCode.AUTHORITY, "item",CountryCode.ITEM); sMatcher.addURI(CountryCode.AUTHORITY, "item/#", CountryCode.ITEM_ID); } ... } @Override public boolean onCreate() { dbHelper = new MyHelper(getContext(), CountryCode.DB_NAME, null,CountryCode.VERSION); return true; } @Override public String getType(Uri uri) { switch (sMatcher.match(uri)) { case CountryCode.ITEM: return CountryCode.CONTENT_TYPE; case CountryCode.ITEM_ID: return CountryCode.CONTENT_ITEM_TYPE; default: throw new IllegalArgumentException("Unknown URI " + uri); } } @Override public int delete(Uri uri, String where, String[] args) { SQLiteDatabase db = dbHelper.getWritableDatabase(); int count; switch (sMatcher.match(uri)) { case CountryCode.ITEM: count = db.delete(CountryCode.TB_NAME, where,args); break; case CountryCode.ITEM_ID: String id = uri.getPathSegments().get(1); count = db.delete(CountryCode.TB_NAME, CountryCode.ID + "=" + id + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), args); break; default: throw new IllegalArgumentException("Unknown URI " + uri); } getContext().getContentResolver().notifyChange(uri, null); return count; } @Override public int update(Uri uri, ContentValues values, String where, String[] args) { SQLiteDatabase db = dbHelper.getWritableDatabase(); int count; switch (sMatcher.match(uri)) { case CountryCode.ITEM: count = db.update(CountryCode.TB_NAME,values, where,args); break; case CountryCode.ITEM_ID: String id = uri.getPathSegments().get(1); count = db.update(CountryCode.TB_NAME,values,CountryCode.ID+"=" + id + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), args); break; default: throw new IllegalArgumentException("Unknown URI " + uri); } getContext().getContentResolver().notifyChange(uri, null); return count; } @Override public Uri insert(Uri uri, ContentValues initialValues) { SQLiteDatabase db = dbHelper.getWritableDatabase(); long rowId; if (sMatcher.match(uri) != CountryCode.ITEM) { throw new IllegalArgumentException("Unknown URI " + uri); } rowId = db.insert(CountryCode.TB_NAME,CountryCode.ID,initialValues); if (rowId > 0) { Uri noteUri = ContentUris.withAppendedId(CountryCode.CONTENT_URI, rowId); getContext().getContentResolver().notifyChange(noteUri, null); return noteUri; } throw new SQLException("Failed to insert row into " + uri); } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] args,String order) { SQLiteDatabase db = dbHelper.getReadableDatabase(); Cursor c; switch (sMatcher.match(uri)) { case CountryCode.ITEM: c = db.query(CountryCode.TB_NAME, projection, selection, args,null,null,order); break; case CountryCode.ITEM_ID: String id = uri.getPathSegments().get(1); c = db.query(CountryCode.TB_NAME, projection, CountryCode.ID + "=" + id + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""), args,null,null,order); break; default: throw new IllegalArgumentException("Unknown URI " + uri); } c.setNotificationUri(getContext().getContentResolver(), uri); return c; }