Pictured above, www.gratisography.com

In the development process, sometimes there will be requirements to achieve data sharing between applications. In the Android system, a ContentProvider is provided to achieve this function, which needs to inherit this class and implement the relevant interface. Other applications can access the relevant data through uri. The most common content provider in Android is MediaProvider. All applications can access the media data in the system through URI, and we can also implement such a function. The first is to determine whether your application should provide data to the outside

Project code

Design data store

Here we are through the database as the medium to provide data, need to design the specific database data structure, through the system provided SQLiteOpenHelper class to create the database core code:

Simple data structure, according to their own needs to design the database data structure

public final class Settings implements BaseColumns{ private Settings(){} public static final String TABLE_NAME = "settings"; Public static final String COLUMN_NAME_TITLE = "name"; Public static final String COLUMN_NAME_VALUE = "value"; // column name in table}Copy the code

Inherit SQLiteOpenHelper to create the database

private static class DatabaseHelper extends SQLiteOpenHelper{ public DatabaseHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS settings"); onCreate(db); } @Override public void onCreate(SQLiteDatabase db) { String sql = " CREATE TABLE "+Settings.TABLE_NAME +" (" + Settings._ID +" INTEGER PRIMARY KEY," + Settings.COLUMN_NAME_TITLE +" TEXT," + Settings.COLUMN_NAME_VALUE +" TEXT" + "); " ; db.execSQL(sql); }}Copy the code

Content provider

The ContentProvider has the following overloaded interfaces:

  • public Uri insert(Uri uri, ContentValues values)
  • public int delete(Uri uri, String selection, String[] selectionArgs)
  • public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)
  • public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
  • public String getType(Uri uri)

    The design of the URI

    [content://][com.example.demo.gank.provider][/settings][/1]

    |—–A—–|—————B—————|—–C—-|–D-|

  • A: Yes, the fixed value is Content ://

  • B: That’s rightContent Provider, usually using the application registration to name
  • C: indicates the path of a resource
  • D: indicates the ID of the resource

    In order to perform different operations on the incoming URI, you can map the URI to different contents through the UriMatcher entity class in Android

Set the permissions

You can set permissions for the provider

.Copy the code

Run the project, the project application is A, and once the A application is installed, the application is A content provider

Let’s create another project B, access the data in B, and this is the code for accessing the database in project B

Uri uri = Uri.parse("content://com.example.demo.gank.provider/settings"); Cursor C = getContentResolver().query(uri,new String[]{"name","value"}," name =? ", // is the database field defined in A new String[]{" Settings "},null); if(c ! = null && c.moveToFirst()){ String value = c.getString(c.getColumnIndex("value")); log.e(TAG,"=================query value: "+value); }Copy the code

Add permissions to androidmanifest.xml ‘in B

  
  Copy the code

The result is the contents of the database in A

code

Core logic code, specific code please refer to the source code project

Settings.java

  public static final String SCHEME = "content://";

  private static final String PATH_SETTINGS = "/settings";
  private static final String PATH_SETTINGS_ID = "/settings/";

  public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.com.example.demo.gank.settings";
  public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.com.example.demo.gank.settings";Copy the code

SettingsProvider.java

public class SettingsProvider extends ContentProvider{ private static final String DB_NAME = "settings.db"; private static final int DB_VERSION = 1; private static final int SETTINGS = 1; private static final int SETTINGS_ID = 2; / /... static { sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); sUriMatcher.addURI(Settings.AUTHORITY,"settings",SETTINGS); sUriMatcher.addURI(Settings.AUTHORITY,"settings/#",SETTINGS_ID); sProjectionMap = new HashMap<>(); sProjectionMap.put(Settings.COLUMN_NAME_TITLE,Settings.COLUMN_NAME_TITLE); sProjectionMap.put(Settings.COLUMN_NAME_VALUE,Settings.COLUMN_NAME_VALUE); sProjectionMap.put(Settings._ID,Settings._ID); } @Nullable @Override public Uri insert(Uri uri, ContentValues values) { return null; } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { return 0; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { return 0; } @Nullable @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { return null; } @Nullable @Override public String getType(Uri uri) { return null; } @Override public boolean onCreate() { mOPenHelper = new DatabaseHelper(getContext()); return true; } private static class DatabaseHelper extends SQLiteOpenHelper{private static class DatabaseHelper extends SQLiteOpenHelper{ }}Copy the code