1.创建一个类继承ContentProvider类并实现里面的方法
public class Mycontentprovider extends ContentProvider {
@Override
public boolean onCreate() {
return false;
}
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
return null;
}
@Nullable
@Override
public String getType(@NonNull Uri uri) {
return null;
}
@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
return null;
}
@Override
public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
return 0;
}
@Override
public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
return 0;
}
}
2.在清单文件里面配置
<provider
android:exported="true"
android:authorities="com.android.provider"
android:name=".Mycontentprovider">
</provider>
3.在静态代码块中添加需要匹配的Uri
static {
mUriMatcher=new UriMatcher(UriMatcher.NO_MATCH);
mUriMatcher.addURI("com.android.provider", "bank", SUCCESS);//添加匹配信息
}
4 使用内容解析者调用
ContentResolver resolver = getContentResolver();//获取内容解析者
Uri uri = Uri.parse("content://com.android.provider/bank");
ContentValues values = new ContentValues();
resolver.insert(uri,values);//这里会调动内容提供者里面的insert方法