Android serial 37 – sharing data across programs


1、 Content provider

  • Use content providers to share data
  • It can accurately control which data can be shared and which data cannot be shared
  • The content provider has two uses: (1) use the existing content provider to read and operate the data in the corresponding program( 2) Create your own content provider to provide external access to our program’s data

2、 Basic usage of contentresolver

  • Method to get contentresolver instance: new context(). Getcontentresolver()
  • This instance provides a series of methods insert(), update(), delete(), query() for CRUD operations
  • These member methods differ somewhat in parameters from sqlitedatabase instances
  • The table name parameter becomes a URI parameter (content URI)
  • A URI consists of connected parts: permissions and paths
  • Permissions are used to distinguish between different programs by package naming. For example, a package iscom.example.appThen the corresponding permission of the program can be
  • Paths are used to distinguish different tables in the same application and are usually added after permissions. For example, if a program contains two tables table1 and table2, the paths can be named / table1 and / table2 respectively. Then combine the two, and the content URL
  • You also need to add a protocol to the headercontent://
  • For this string, we need to parse it into a URI object to pass it in as a parameter
Uri uri = Uri.parse("content://");
  • The query code is as follows
Cursor cursor = getContentResolver().query(uri,projection,selection,selectionArgs,sortOrder);
  • We can make a comparison of these parameters at a glance
Query() method parameters Corresponding SQL part describe
uri from table_name Specify to query a table under an application
projection select colum1,column2 Specifies the column name of the query
selection where column = value Specify constraints for where
selectionArgs Provide specific values for placeholders in where
orderBy order by column1,column2 Specifies how query results are sorted
  • After the query, a cursor object is returned. Next, we read the data from the cursor object one by one. The reading idea is still to traverse all rows of the cursor by moving the cursor position, and then take out the data of the corresponding column in each row
if(cursor != null ){ while(cursor.moveToNext(){  String column1 = cursor.getString(cursor.getColumnIndex("column1"));  int column2 = cursor.getInt(cursor.getColumnIndex("column2")); } cursor.close();}
  • The remaining additions and deletions should not be difficult
ContentValues values = new ContentValues();values.put("column1","text");values.put("column2","text");getContentResolver().insert(uri,values);
  • Insert data at the top and update data at the bottom
ContentValues values = new ContentValues();values.put("column1","");getContentResolver().update(uri,values,"column1 = ? and column2 = ?",new String[] {"text","1"});
  • Delete data
getContentResolver().delete(uri,"column2 = ?",new String[]{"1"});

3、 Source code: