I. Overview of Content Provider
Content provider is used to save and retrieve data and make it visible to all applications, which is the only way to share data between different applications, because there is no common storage area available in Android that all apps can access
1, content provider internal data How to save is determined by its designers, and all of the content provider to achieve a common set of methods to provide data deletion and modification of the operation
2, the client if you want to use these methods of operation, the ContentProvider object can be implemented to the Content provider, and developers can call Getcontentresolver () method to get the ContentProvider object
i.e.: contentresolver CR = Getcontentprovider ();
3. Communication between different processes is handled by the Contentresolver class and the ContentProvider class
II. Content Provider data model to provide data
|
_id |
NAME |
Age |
|
001 |
Alice |
17 |
|
002 |
Mark |
20 |
|
003 |
Tom |
11 |
You can see that you are using a simple table based on the database model to provide the data rows: Record columns: Fields
* Each record contains a _id field that uniquely identifies the record in the table
The ID field contains an underscore _, which is a must-have and cannot be written
Third, URI
1. Each content provider provides a common URI (using the URI class wrapper) to uniquely identify its dataset
2. Uri constants are used for all content provider interactions, and each content provider method uses a URI as the first parameter of the method, which is used to identify the contentresolver should use the content Which data table in the provider
3, uri:content://com.xqx.mycontent/dba/010
(1) content://: Standard prefix, used to identify the data managed by the content provider, fixed value, without modification
(2) The authority part of Com.xqx.mycontent:URI, which identifies the content Provider, declares the authority in the Authority attribute of the <provider> element of the manifest file, For third-party apps, this section is the full class name (all lowercase letters)
(3)/DBA: The path section, which determines what kind of data is requested, and if content provider only provides one data type, it can be omitted, and if multiple types are provided, it is made up of multipart
(4)/010: The ID of the particular record being requested, if the requested record is not limited to a single record (for example, querying all fields), the part does not have to be written
Iv. pre-defined content Provider
1, Android system provides a lot of predefined content Provider (voice, contacts, call logs, etc.)
Eg:
Browser: Reading or modifying bookmarks, browsing history or Web search
Calllog: View or Update call history
Contacts:: Get Modify or Save contact information
Mediastore: Access to pictures, videos, music, etc.
2. Query data
If you want to query content provider data, you need three information
(1) URI that identifies the content provider
(2) Name of the data field that needs to be queried
(3) Type of field value data
* If you are querying a specific record, you also need to provide the ID value of the record
To limit the return of one piece of data, you can add the ID of the record you want to manipulate at the end of the URI.
content://..../003
You can implement this feature by using the static method Contenturis.withappendedid (), which returns a URI object with an increased ID
Getting data is handled with the cursor object, which can traverse the entire query result set forward or backward.
3. Add record
(1) To add data to ContentProvider, you need to use the Contentvalues object to establish a key-value pair mapping, a field in the keyed content provider, and a value to add
(2) Call Contentresolver.insert (Uri,value);
The first parameter is the URI to be manipulated by the ContentProvider uniquely identified, the second parameter is the contentvalues mapping, and the method returns the complete URI of the added record
4. Delete records
(1) You need to call int delete (URI URL, String where, string[] Selectionargs) method, the first is the Uri to be manipulated, the second is the condition of the deletion, the third parameter is the padding value
int delete (URI URL, "_id=?", 11) to delete a record with ID 11 in the URI
Five, custom content Provider
1, we learned from the above that developers can use the system given Contentprovide
Of course, we can also customize the ContentProvider to share our own data (of course, you can also use the system predefined ContentProvider, manage the same data and write permissions, can also modify the corresponding data)
customizing ContentProvider steps
(1), set up data storage System
Android provides Sqliteopenhelper class help to create a database, Sqlitedatabase class to help manage the database
(2), inheriting ContentProvider
Need to rewrite 6 abstract methods
|
Method |
Description |
|
|
For initializing provider |
|
Query () |
Return data to caller |
|
in SERT () |
Insert new data to ContentProvider |
|
Updata () |
Update data |
|
Delete () |
Delete data from ContentProvider |
|
GetType () |
Returns the MIME type of the Cntentprovider data |
(3), register content Provider in the application's Androidmanifest file
1<!--Registering ContentProvider Components2 Android:authorities: Declares the unique identity of the component3 android:permission: Declaring permissions for this component4Android:exported="true": Declares that the component can be accessed by external applications5-6<provider7Android:name="Com.xqx.UserContentProvider"8android:authorities="com.xqx.users"9android:permission="Com.xqx.READ_WRITE"TenAndroid:exported="true" One/>
-----------------------------------------------------------------------------------------------
Android Development _ in-depth understanding of content Provider