ContentResolver:当外部应用需要对ContentProvider中的数据进行添加、删除、修改和查询操作时,可以使用ContentResolver 类来完成,要获取ContentResolver 对象,可以使用Activity提供的getContentResolver()方法。 ContentResolver使用insert、delete、update、query方法,来操作数据。
四、ContentProvider示例程序
Manifest.xml中的代码:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="net.loonggg.test" 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" > <activity android:name="net.loonggg.test.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> <provider android:name="net.loonggg.service.PersonContentProvider" android:authorities="net.loonggg.test.personprovider" > </provider> </application> </manifest>需要在<application></application>中为provider进行注册!!!!
这里用到了数据库的内容,而与数据库相关的内容我用的就是在Android学习记录(2)—Android中数据库的常见操作中的内容,所以我不讲了,直接讲与ContentProvider直接的内容,待会我会把代码放出来,让大家参考,所以这里不过多赘述了!
1、接着创建一个PersonContentProvider.java对数据库的接口进行包装,让数据库的操作暴露出来:
package net.loonggg.service; import net.loonggg.db.DBHelper; import net.loonggg.db.DBInfo.Table; 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 PersonContentProvider extends ContentProvider { // 定义一个uri的匹配器,用于匹配uri,如果路径不满足条件返回 -1 private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); private static final int INSERT = 1; private static final int DELETE = 2; private static final int UPDATE = 3; private static final int QUERY = 4; private static final int QUERYONE = 5; private DBHelper dbHelper = null; static { // 添加一组匹配规则 matcher.addURI("net.loonggg.test.personprovider", "insert", INSERT); matcher.addURI("net.loonggg.test.personprovider", "delete", DELETE); matcher.addURI("net.loonggg.test.personprovider", "update", UPDATE); matcher.addURI("net.loonggg.test.personprovider", "query", QUERY); matcher.addURI("net.loonggg.test.personprovider", "query/#", QUERYONE); } // content://net.loonggg.test.personprovider/insert 添加的操作 // content://net.loonggg.test.personprovider/delete 删除 // content://net.loonggg.test.personprovider/update 更新 // content://net.loonggg.test.personprovider/query 查询 /** * 当内容提供者初始化的事后调用,适合数据化的初始化 */ @Override public boolean onCreate() { dbHelper = new DBHelper(getContext()); return false; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { if (matcher.match(uri) == QUERY) { SQLiteDatabase db = dbHelper.getReadableDatabase(); Cursor cursor = db.query(Table.USER_INFO_TB_NAME, projection, selection, selectionArgs, null, null, null); return cursor; } else if (matcher.match(uri) == QUERYONE) { SQLiteDatabase db = dbHelper.getReadableDatabase(); long id = ContentUris.parseId(uri); Cursor cursor = db.query(Table.USER_INFO_TB_NAME, projection, "userId = ?", new String[] { id + "" }, null, null, null); return cursor; } else { throw new IllegalArgumentException("路径不匹配,不能执行查询操作"); } } @Override public String getType(Uri uri) { if (matcher.match(uri) == QUERY) { return "vnd.android.cursor.dir/person"; } else if (matcher.match(uri) == QUERYONE) { return "vnd.android.cursor.item/person"; } return null; } @Override public Uri insert(Uri uri, ContentValues values) { if (matcher.match(uri) == INSERT) { SQLiteDatabase db = dbHelper.getWritableDatabase(); db.insert(Table.USER_INFO_TB_NAME, null, values); } else { throw new IllegalArgumentException("路径不匹配,不能执行插入操作"); } return null; } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { int result = 0; if (matcher.match(uri) == DELETE) { SQLiteDatabase db = dbHelper.getWritableDatabase(); result = db.delete(Table.USER_INFO_TB_NAME, selection, selectionArgs); } else { throw new IllegalArgumentException("路径不匹配,不能执行删除操作"); } return result; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { int result = 0; if (matcher.match(uri) == UPDATE) { SQLiteDatabase db = dbHelper.getWritableDatabase(); result = db.update(Table.USER_INFO_TB_NAME, values, selection, selectionArgs); } else { throw new IllegalArgumentException("路径不匹配,不能执行更新操作"); } return result; } }到这里关于ContentProvider的内容就差不多完了,这样数据就暴露出来了,可以运行程序了,最后写一个第三方的应用程序就可以操作这个程序里的数据了!
第三方调用本程序内容的程序,我也会把代码打包一同放出来,让大家参考。
代码下载地址:http://yunpan.cn/QDRj7dc3Gv2dn