Content Provider Architecture
AuthoritySimilar to the domain name in the web, each content provider registers authority with the system through AndroidManifest. xml, as shown below. Here, name is the class name, that is, how to find this content
Provider. You can save the package name in AndroidManifest. xml without writing the complete class name. For example, android: name = ". BookProvider ".
<ProviderAndroid: name = "SomeProvider"Android: authorities= "Com. your-company.SomeProvider"/>
<ProviderAndroid: name = "NotePadProvider"Android: authorities= "Com. google. provider. NotePad"/>
Similar to the web URL domain name, the URL of the content provider for data access isContent: // authority /......, Such as content: // com. your-company.SomeProvider /. For content providers provided by Android, it is sometimes easier to write. For example, contacts is used to replace com. google. android. contacts, such as content: // contacts /.......
Sometimes the registered provider is complicated. For example, the Android contact information, whose uri is content: // com. android. contacts/contacts. The source code is as follows. This provider requires read and write permissions.
<Provider android: name = "ContactsProvider2"
Android: authorities = "contacts; com. android. contacts"
Android: label = "@ string/provider_label"
Android: multiprocess = "false"
Android: readPermission = "android. permission. READ_CONTACTS"
Android: writePermission = "android. permission. WRITE_CONTACTS">
<Path-permission
Android: pathPrefix = "/search_suggest_query"
Android: readPermission = "android. permission. GLOBAL_SEARCH"/>
<Path-permission
Android: pathPrefix = "/search_suggest_shortcut"
Android: readPermission = "android. permission. GLOBAL_SEARCH"/>
<Path-permission
Android: pathPattern = "/contacts/. */photo"
Android: readPermission = "android. permission. GLOBAL_SEARCH"/>
<Grant-uri-permission android: pathPattern = ". *"/>
</Provider>
Content URI Structure. Android obtains data through Content URI and returns a cursor with a row-column structure. The content UI format is:
Content: // <Authority-name>/<Path-segment1>/<Path-segment2>/Etc...
Example: content: // com. google. provider. NotePad/notes/23
In the example,/notes indicates collection, or is understood as a directory called path segment, while/23 indicates a specific item, which is a specific index. content provider provides two-dimensional data, this is the _ id value of the row.
MIME Type. The HTTP response will contain the MIME Type, which is text/html at the bottom, to inform the body of the data Type. The same is true for Content providers. MIME can be obtained using methods. MIME consists of two parts: type/subtype. For details, refer to rfc2046. The definitions of type and subtype can be found in IANA. Below are some examples of MIME:
Text/xml
Application/rtf
Application/vnd. ms-excel // vnd is vendeor-specific. The custom format of the manufacturer is Microsoft excel format.
Application/x-tar // x-indicates the custom private format
There can be multi-level directories in the Content Provider, that is, there are item and collection, corresponding to the MIME tye of item and the MIME type of collection respectively. Android uses namespace to define type and subtype. As follows:
Vnd. android. cursor. item/vnd. <Yourcompanyname. contenttype>Is the MIME type of item
Vnd. android. cursor. dir/vnd. <Yourcompanyname. contenttype> is the MIME type of the collection.
From the preceding format, we can see that the type has been specified and the developer can only set the subtype.
Clear description. We should provide a clear definition or description for the created content provider, and you can use the Uri to predefine the constant. For example, MediaStore. Images. Media. INTERNETAL_CONTENT_URI indicates content: // media/internal/images. At the same time, we should also be a column
Example: Reading contact information. The Provider accesses the data through the uri and returns the cursor (cursor). We will use the following small example for verification. First, we will understand the Uri and then perform secondary round-robin on the cursor, use the for method at one time. As shown in the preceding definition of contact provider in xml, there are read and write permissions, so the corresponding permissions should be granted in XML:<Uses-permission
Android: name = "android. permission. READ_CONTACTS"/>
Private void contentprovidertest (){ /* There are a lot of contact content. We only select some column names, which is equivalent to xxxx in select XXXX from. If it is a self-created content provider, you should also use the constant method to clearly express the content of each item */
String [] contactprojection = new string [] { Contacts. _ id,/* each row has a unique _ id to indicate */
Contacts. display_name_primary }; /* Below are some uri operations. As a test, the Uri through the provider will provide the constant Method */
Uri peopleBaseUri = ContactsContract. Contacts. CONTENT_URI; ShowInfo (lelebaseuri. toString ()); // Display showInfo () information. The content will be displayed in TextView. Uri myPersonUri = Uri. withAppendedPath (lelebaseuri, "1 "); ShowInfo (myPersonUri. toString ());
/* ManagedQuery () reads data from the Content Provider and returns Cursor
* 1st parameter: Uri uri indicates URI; * 2nd parameter: String [] projection indicates the information to be read; * 3rd parameter: the number of String selection is a restriction. It is similar to WHERE in SQL, but "WHERE" is removed "; * 4th parameter: String [] selectionArgs and 3rd parameters are used in combination. Specifically, the "?" parameter in the third parameter is described. Why; * 5th parameter: String sortOrder, similar to order by */in SQL */ @ SuppressWarnings ("deprecation ") // CursorLoader is used after API Level 11, that is, Android3.0. In order not to warn Eclipse, We will disable the warning here. I can see that Eclipse has a small triangle alarm description, and I feel uncomfortable. I will take it out. Pai_^
Cursor cur =ManagedQuery(Contactscontract. Contacts. content_uri,
Contactprojection, Null, Null, Null ); Showinfo ("query contacts get cursor:" + cur );
Showinfo ("cursor has" + cur. getcount () + "rows .");
// Round-robin Method 1: Showinfo ("read from cursor "); // A cursor is a collection of rows. You must first use moveToFirst (), because the cursor is located before the first row after query. Returns false, indicating that it is null.
If (! Cur.MoveToFirst()){ Showinfo ("No rows. It's empty "); Cur. Close (); Return; }
// The cursor can be moved before and after, to view the data of different rows, you can also specify a Specific Row, It is very flexible to move. The cursor obtains data based on column number, which can be obtained by column names.
Int nameColumnIndex = cur.GetColumnIndex(Contacts. DISPLAY_NAME_PRIMARY );
ShowInfo ("\ t Name:" + cur.GetString(NameColumnIndex ));
While (cur.MoveToNext()){ ShowInfo ("\ t Name:" + cur. getString (nameColumnIndex ));
}
// Round robin Method 2: ShowInfo ("read from cursor again "); For (cur. moveToFirst ();! Cur. isAfterLast (); cur. moveToNext ()){ String name = cur. getString (nameColumnIndex ); ShowInfo ("\ t Name:" + name ); }
Cur.Close(); } |
Where condition usage. You can use Uri or manageQuery () to search for specific data. For example, if you want to query select * from notes where _ id = 23, you can set
String noteUri = "content: // com. google. progider. NotePad/notes/23 ";
The number of selection erasers in manageQuery () can also be expressed
ManagedQuery (uri, // "content: // com. google. provider. NotePad/notes"
Null,
"_ Id =? ",
New String [] {23},
Null );
Add, modify, and delete data. Addition, deletion, modification, and query are the four functions of Data Reading and Writing. We will give a detailed description in the following small example.
Related links:
My Android development articles