preface

As one of the four components, its status cannot be taken lightly. But where have we ever used him? In fact, it is very simple, when you use the app, do you often ask you whether to open the address book access, if you agree, this time the ContentProvider plays its role.

directory

  • The Activity chapter is a must-know component of Android
  • Android must know will be four components — ContentProvider
  • The Broadcast Receiver is a must-know component of Android
  • Android must know must know four components — Service

Mind mapping

Method of use

The following operations through the address book to let the reader to understand more clearly. However, shared data should not be arbitrarily modified, and if necessary, it is more appropriate to store the data locally and manipulate it later, so the demo code below contains only query functionality. However, since calling external data generally requires permission requests.

I have integrated the permission application utility class into the Android toolkit.

// Data query
try (Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null.null.null.null)) {
        while (cursor.moveToNext()) {
                String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                Log.e(TAG, name + ";"+ number); }}catch (Exception e) {
            e.printStackTrace();
}
Copy the code

The basics of file sharing

In fact, the communication mechanism used in ContentProvider is still Binder, and file location is done by URI, so part of the lecture is URI format parsing.

URI

Format: [scheme:] [/ / host: port] [path] [query]?Copy the code

For the sake of convenience, let’s analyze the format of the URI directly by taking a domain name.

(2).

Link address:Juejin. Cn/user / 888061…

  1. The scheme:httpsThat is, the agreement
  2. Host:juejin.im, domain name address
  3. Port: the default port number is 80
  4. Path:/user/5e2659e15188254d95242d4b, file path/controller path
  5. Query: such as? userId=x&message=yThat’s usjavaWebSome request data in.

Of course in ourContentProviderThere is a certain deviation.

File location: the content: / / com. Clericyi. The file/message/id

  1. The scheme:content://This is the fixed path for Android
  2. Authority:com.clericyi.fileThat is, to indicate the uniqueContentProvider
  3. Path:message, which is the corresponding table name
  4. Id: indicates the data in the corresponding table

Help tool

UriMatcher

AddURI () and match() are two open methods that help match urIs in ContentProvider by removing the first half of the ID.

/ / usage
UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
String authority = "com.clericyi.file";
String path = "message";
int URI_CODE = 1;
// Associate the URI with URI_CODE
uriMatcher.addURI(authority, path, URI_CODE);
// Get the corresponding URI_CODE
uriMatcher.match(Uri.parse("content://com.clericyi.file/message"))
Copy the code

ContentUris

It is also a built-in utility class that provides methods such as parseId(), appendId(), withAppendedId(), removeId() for IDS

Uri uri = Uri.parse("content://com.clericyi.file/messag");
/ / connection id
uri = ContentUris.withAppendedId(uri, CODE);
/ / remove the id
uri = ContentUris.removeId(uri);
/ / to get id
long num = ContentUris.parseId(uri);
Copy the code

ContentProvider code flow guide

Workflow:

(1) Call query() to get a ContentResolver with many internal parameters, which are basically the same as the database query. AcquireUnstableProvider (URI) is called by the Binder () method and IContentProvider (uri) is returned by the Binder. And then we're going to convert it to our Cursor and we're going to slide it around and read it, and it's going to convert it to our dataCopy the code

Query () internal parameter analysis

  • Uri: A unique representation of location exposure.
  • Projection: returns columns (fields)
  • Selection: Sets a condition, which is equivalent to where in the database
  • SelectionArgs: used in conjunction with Selection to replace?
  • SortOrder: sortOrder, equivalent to order by in the database
All the parameters using the instance: contentResolver. The query (android. The provider. ContactsContract. Contacts. CONTENT_URI,new String[]{android.provider.ContactsContract.Contacts.DISPLAY_NAME}
    , android.provider.ContactsContract.Contacts.DISPLAY_NAME + "=?"
    , new String[]{"A little"}
    , android.provider.ContactsContract.Contacts.DISPLAY_NAME + " DESC"); // There is a space in the middle, default is ASC, ascending order.
Copy the code

At the end of this article, I will not write about the use of federated Database, which is easy to be lengthy, but I will find a special article for readers to learn the use of ContentProvider. Skip links

In addition, it was also a breakthrough for me. After all, I posted four blogs in one day. Although the two blogs were actually just a reconstruction, it was still a bit tired.