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

Step:

(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
     */
    @Override
    public boolean onCreate() {
        openHelper = new MyOpenHelper(getContext());
        database = openHelper.getWritableDatabase();
        if(database != null)
            return true;
        else
            return false;
    }

    @Nullable
    @Override
    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
     */
    @Nullable
    @Override
    public String getType( @NonNull Uri uri) {
        return null;
    }


    @Nullable
    @Override
    public Uri insert( @NonNull Uri uri,  @Nullable ContentValues values) {
         database.insert("user",null,values);
         return uri;
    }

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

    @Override
    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
		<provider
            android:authorities="com.bawei.1804A"
            android:name=".MyContentProvider"
            android:exported="true"/>

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

2. Content requester

explain:

  • 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;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

    }

    public void read(View view) {
        ContentValues values = new ContentValues();
        values.put("uname","ck");
        resolver.insert(uri,values);
    }

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

    public void deleteUser(View view) {
        resolver.delete(uri,null,null);
    }

    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"));
            Log.d("amy",uid+"-"+uname+"-"+age+"-"+sex+"-"+birthday);
        }
    }
}

4, Custom permissions

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

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


2. Permissions in the list file of the content requester

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

Topics: Android Database Mobile