ContentProvider实现数据共享[总结]
OK,ContextProvider测试完成,总结下。
这个东西出现的意义是为了一个系统中,多应用间共享数据?
那不有文件,xml形式吗?嗯,当然,这也是种方式。
思路:
1.provider部署建立
a).继承ContextProvider这个抽象基类
b).ContextProvider这玩意是个抽象类,CURD函数都是空实现,等你来实现的。
c).实现ContextProvider CURD函数,根据uri.
d).这中有个很重要的uri匹配对象,UriMatcher
e).Manifest清单中配置好provider,在系统中,为此应用添加自定义访问权限(安全起见)
2.resolver调用
a).获取ContentResolver对象,调用CURD api即可。
1.provider部署建立
源码示例
[
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.buluo.contenproviderDemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.android.buluo.contenproviderDemo" >
</instrumentation>
<!-- 在系统中,为此应用添加自定义访问权限(安全起见) -->
<permission android:name="contenproviderDemo.read" />
<permission android:name="contenproviderDemo.write" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="android.test.runner" />
<activity
android:name="com.android.buluo.contenproviderDemo.BlackActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.android.buluo.contenproviderDemo.MainActivity2"
android:label="@string/app_name" >
</activity>
<provider
android:name="com.android.buluo.providers.DictContentProvider"
android:authorities="com.android.buluo.providers.DictContentProvider"
android:exported="true"
android:readPermission="contenproviderDemo.read"
android:writePermission="contenproviderDemo.write" >
</provider>
</application>
</manifest>
]
[
public class DictContentProvider extends ContentProvider {
// uriMatcher
private static final UriMatcher uriMatcher = new UriMatcher(
UriMatcher.NO_MATCH);
// AUTHORITY
private static final String AUTHORITY = "com.android.buluo.providers.DictContentProvider";
private static final String DICT_QUERY_ALL = "dict/query_all";
private static final String DICT_QUERY_ITEM = "dict/query_item";
private static final String DICT_DELETE = "dict/delete_all";
private static final String DICT_DELETE_ITEM = "dict/delete_item";
// 相应标识码
private static final int DICT_QUERY_ALL_CODE = 0;
private static final int DICT_QUERY_ITEM_CODE = 1;
private static final int DICT_DELETE_CODE = 2;
private static final int DICT_DELETE_ITEM_CODE = 3;
static {
// 注入uri
uriMatcher.addURI(AUTHORITY, DICT_QUERY_ALL, DICT_QUERY_ALL_CODE);
uriMatcher.addURI(AUTHORITY, DICT_QUERY_ITEM, DICT_QUERY_ITEM_CODE);
uriMatcher.addURI(AUTHORITY, DICT_DELETE, DICT_DELETE_CODE);
uriMatcher.addURI(AUTHORITY, DICT_DELETE_ITEM, DICT_DELETE_ITEM_CODE);
}
@Override
public boolean onCreate()
{
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder)
{
switch (uriMatcher.match(uri)) {
case DICT_QUERY_ALL_CODE:
return null;
case DICT_QUERY_ITEM_CODE:
return null;
default:
break;
}
return null;
}
@Override
public String getType(Uri uri)
{
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values)
{
return null;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs)
{
switch (uriMatcher.match(uri)) {
case DICT_DELETE_CODE:
return uriMatcher.match(uri);
default:
throw new IllegalArgumentException("url不匹配:" + uri);
}
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs)
{
return 0;
}
}
]
2.resolver调用
源码示例
[
public class TestCase extends AndroidTestCase {
private static final String TAG = "TestCase";
public void test_query()
{
Uri uri = Uri.parse("content://com.android.buluo.providers.DictContentProvider/dict/query_all");
ContentResolver resolver = this.getContext().getContentResolver();
Cursor cursor = resolver.query(uri, null, null, null, null);
Log.i(TAG, "test_query返回的应该是null:" + cursor);
}
public void test_delete()
{
Uri uri = Uri.parse("content://com.android.buluo.providers.DictContentProvider/dict/delete_all");
ContentResolver resolver = this.getContext().getContentResolver();
int delete_code = resolver.delete(uri, null, null);
Log.i(TAG, "test_delete返回的code应该是2>:" + delete_code);
}
}
]