1.寫一個類繼承于ContentProvider
private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
static {
matcher.addURI("com.contentprovider", "xaioer", SUCCESS);
}
@Override
public Uri insert(Uri uri, ContentValues values) {
if (matcher.match(uri)== SUCCESS) {
System.out.println("成功了");
}else {
throw new RuntimeException("出錯了琴拧;額");
}
return uri;
}
2.xml中定義 authorities用來進行認(rèn)證
<provider android:name="com.example.contentprovider.ProvideService"
android:authorities="com.contentprovider" >
3.設(shè)置守衛(wèi):
private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
static {
matcher.addURI("com.contentprovider", "xaioer", SUCCESS);
}
4.驗證守衛(wèi)的code
public Uri insert(Uri uri, ContentValues values) {
if (matcher.match(uri)== SUCCESS) {
System.out.println("成功了");
}else {
throw new RuntimeException("出錯了酥郭;額");
}
return uri;
}
5.其他程序進行訪問
ContentResolver contentResolver = getContentResolver();
Uri uri = Uri.parse("content://com.contentprovider/xaioer");
ContentValues values = new ContentValues();
contentResolver.insert(uri, values);