http://my.eoe.cn/zhxit/archive/4739.html
1 2 3 4 |
当我们需要像其他应用开放数据时,可以通过contentprovider来实现。
1、contentprovider操作与数据库类似,提供增、删、改、查操作。实现contentprovider时需要重写父类的提供的这四个方法,来实现向外暴露数据,或外部修改内部数据。
2、而为了满足外部与contentprovider交互时,contentprivider可以区别用户提供的uri,实现contentprovider时需要考虑向外提供的uri,以此来区别用户提供的uri。这些都通过UriMatcher来实现。即用户访问的uri必须是你已经提供处理的,否则没法处理。
|
3、具体见代码:
1 |
```package com.example.gettempermission;
|
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;
public class CustomContentProvider extends ContentProvider {
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
private static final String TAG = "TEST";
private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
private static final String authority = "com.example.gettempermission.provider";
private static final String sceme = "content://";
private static final int PERSON = 1;
private static final int PERSON_ID = 2;
private static final int PHONE = 3;
private static final int PHONE_ID = 4;
private static final String TABLE_PERSON = "person";
private static final String TABLE_PHONE = "phone";
static {
uriMatcher.addURI(authority, "person", PERSON);
uriMatcher.addURI(authority, "person/#", PERSON_ID);
uriMatcher.addURI(authority, "phone", PHONE);
uriMatcher.addURI(authority, "phone/#", PHONE_ID);
}
private DBHelper helper;
private SQLiteDatabase db;
public CustomContentProvider() {
}
@Override
public boolean onCreate() {
helper = new DBHelper(getContext(), "test", null, 1);
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, //
String[] selectionArgs,//
String sortOrder) {
db = helper.getReadableDatabase();
Cursor cursor = null;
switch (uriMatcher.match(uri)) {
case PERSON:
cursor = db.query(false, TABLE_PERSON, projection, selection,//
selectionArgs,//
null, null, sortOrder, null, null);
break;
case PERSON_ID:
String person_id = uri.getPathSegments().get(1); // 获取id
Log.d(TAG, " query ----> person_id : " + person_id);
String selec = "id = ? ";
cursor = db.query(false, TABLE_PERSON, projection, selec,//
new String[] { person_id },//
null, null, sortOrder, null, null);
break;
case PHONE:
cursor = db.query(false, TABLE_PHONE, projection, selection,//
selectionArgs,//
null, null, sortOrder, null, null);
break;
case PHONE_ID:
String phone_id = uri.getPathSegments().get(1); // 获取id
Log.d(TAG, " query ----> phone_id : " + phone_id);
String phone_selec = "id = ? ";
cursor = db.query(false, TABLE_PERSON, projection, phone_selec,//
new String[] { phone_id },//
null, null, sortOrder, null, null);
break;
default:
break;
}
return cursor;
}
@Override
public String getType(Uri uri) {
int match = uriMatcher.match(uri);
switch (match) {
case PERSON:
return "vnd.android.cursor.dir/vnd.com.example.gettempermission.provider.person";
case PERSON_ID:
return "vnd.android.cursor.item/vnd.com.example.gettempermission.provider.person";
case PHONE:
return "vnd.android.cursor.dir/vnd.com.example.gettempermission.provider.phone";
case PHONE_ID:
return "vnd.android.cursor.item/vnd.com.example.gettempermission.provider.phone";
default:
break;
}
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
db = helper.getWritableDatabase();
Uri resultUri = null;
switch (uriMatcher.match(uri)) {
case PERSON:
long perId = db.insert(TABLE_PERSON, null, values);
String pathUri = sceme + authority + "/" + TABLE_PERSON + "/" + perId;
resultUri = Uri.parse(pathUri);
break;
case PERSON_ID:
String person_id = uri.getPathSegments().get(1); // 获取id
break;
case PHONE:
long phoId = db.insert(TABLE_PHONE, null, values);
String strUri = sceme + authority + "/" + TABLE_PHONE + "/" + phoId;
// ContentUris.appendId(Uri.Builder, phoId);
resultUri = Uri.parse(strUri);
break;
case PHONE_ID:
break;
default:
break;
}
getContext().getContentResolver().notifyChange(resultUri, null);
return resultUri;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return 0;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
return 0;
}
|
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
4、代码实现contentprovider后需要自manifest中注册contentprovider,
a、 配置时需要制定一个android:authorities属性,作为判别contentprovider的id。
同时可以制定访问这个contentprovider的读写权限。 android:readPermission/ android:writePermission
b、也可以指定访问每一个具体项时的权限,来作为获取临时权限使用,<path-permission /> 和 <grant-uri-permission />
<provider
android:name=".CustomContentProvider"
android:authorities="com.example.gettempermission.provider"
android:readPermission="com.example.gettempermission.provider.READ_PERMISSON"
android:writePermission="com.example.gettempermission.provider.WRITE_PERMISSON" >
<path-permission
android:path="content://com.example.gettempermission.provider/person/1"
android:permission="com.example.gettempermission.provider.PATH_READ_WRITE_PERMISSON" />
<grant-uri-permission android:path="content://com.example.gettempermission.provider/person" />
</provider>
5、外部程序访问。
对于设定了权限的contentprovider,我们访问时需要申请权限,经过实践发现需要这样申请:
<uses-permission android:name="com.example.gettempermission.provider.READ_PERMISSON" />
<uses-permission android:name="com.example.gettempermission.provider.WRITE_PERMISSON" />
<permission android:name="com.example.gettempermission.provider.READ_PERMISSON" />
<permission android:name="com.example.gettempermission.provider.WRITE_PERMISSON" />
即<uses-permission />和 <permission/>要同时使用,否则仍然报需要权限异常。
|