Custom content provider

Posted by ericm on Wed, 24 Jun 2020 04:25:53 +0200

Custom content provider

1, What is content provider

The content provider component provides data from one application to another by request.

2, Content provider role

1. Content provider

2. Content resolver

Note: when obtaining mobile phone contact, SMS, audio and other documents through content provider, the provider is the target APP, while we are only the operation requester

3, Custom content provider

1. Custom content provider


(1) Custom class inherits ContentProvider and rewrites its methods
  • onCreate(): used to determine whether the database has been obtained
  • insert()
  • delete()
  • update()
  • query()
  • getType(): the return type of this method. You can know whether the data in Cursor is one or more by using the class. Generally, you can return null
public class MyContentProvider extends ContentProvider {

    MyOpenHelper openHelper;
    SQLiteDatabase database;

     * Get database
     * @return
    public boolean onCreate() {
        openHelper = new MyOpenHelper(getContext());
        database = openHelper.getWritableDatabase();
        if(database != null)
            return true;
            return false;

    public Cursor query( @NonNull Uri uri, @Nullable String[] projection,  @Nullable String selection,  @Nullable String[] selectionArgs,  @Nullable String sortOrder) {
        return database.query("user",projection,selection,selectionArgs,null,null,sortOrder);

     * Return one data or multiple data
     * @param uri
     * @return
    public String getType( @NonNull Uri uri) {
        return null;

    public Uri insert( @NonNull Uri uri,  @Nullable ContentValues values) {
         return uri;

    public int delete( @NonNull Uri uri,  @Nullable String selection,  @Nullable String[] selectionArgs) {
        return database.delete("user",selection,selectionArgs);

    public int update( @NonNull Uri uri,  @Nullable ContentValues values,  @Nullable String selection,  @Nullable String[] selectionArgs) {
        return database.update("user",values,selection,selectionArgs);
(2) Manifest file registration content provider

explain: android:authorities That's the uri address android:exported=true Can be accessed by other app s

2. Content requester


  • Uri uri = Uri.parse("content: / /, followed by the value in the provider manifest file";, where "content: / /" must be added
  • Content requesters can create new projects to write, so as to better experience the role of content providers across app s
public class MainActivity extends AppCompatActivity {

    Uri uri;
    ContentResolver resolver;

    protected void onCreate(Bundle savedInstanceState) {

        uri = Uri.parse("content://com.bawei.1804A");
        resolver = getContentResolver();


    public void read(View view) {
        ContentValues values = new ContentValues();

    public void updateUser(View view) {
        ContentValues values = new ContentValues();
        values.put("birthday",new Date().toString());
        resolver.update(uri,values,"uname = ?",new String[]{"ck"});

    public void deleteUser(View view) {

    public void queryUser(View view) {
        Cursor cursor = resolver.query(uri,null,null,null,null);
        while (cursor.moveToNext()){
            int uid = cursor.getInt(cursor.getColumnIndex("uid"));
            String uname = cursor.getString(cursor.getColumnIndex("uname"));
            int age = cursor.getInt(cursor.getColumnIndex("age"));
            String sex = cursor.getString(cursor.getColumnIndex("sex"));
            String birthday = cursor.getString(cursor.getColumnIndex("birthday"));

4, Custom permissions

1. Permissions declared in the content provider's manifest file

 <permission android:name=""/>
    <permission android:name="bawei.1804.write"/>

2. Permissions in the list file of the content requester

 <uses-permission android:name=""/>
    <uses-permission android:name="bawei.1804.write"/>

Topics: Android Database Mobile