Content provider,
Content Provider
 
Refer:
 
Content provider of four Android components -- ContentProvider-java Xiaobing-CSDN blog
Http://blog.csdn.net/wodewutai17quiet/article/details/46670597
 
Content provider of four Android components -- ContentProvider
 
 
Program B accesses the data of the contact program through the content provider.
 
 
 
1. What is ContentProvider?
 
ContentProvider shares the data in the application with other applications and provides the addition, deletion, modification, and query methods.
 
ContentProvider unifies the data access mode and does not need to adopt different access policies for different data types.
 
ContentProvider encapsulates data and only exposes the data we want to provide to other programs.
 
You can register an observer in ContentProvider to listen for data changes.
 
2. How to create
 
2.1 The definition class inherits the ContentProvider and implements abstract methods. The Abstract METHODS support addition, deletion, modification, and query of database operations.
 
2.2 register in the inventory file: configure the <provider> label under the <application> node in the inventory file. The name and authorities attributes must be specified in the label.
 
Name: complete class name.You can omit the package name (package Value of the manifest node). Note: The omitted class name starts.
 
Authorities: HostIs the path used to access the Provider. It must be unique.
 
 
 
 
 
Program B needs to use the com. baidu url to access and modify and read the contact program.
 
The complete class name is the complete class name that inherits the ContentProvider class.
 
This configuration also tells the computer that my class is a content provider and will be accessed by others later, and I can be accessed through the host com. baidu.
 
 
In program B, we access the contact program through the content parser and host name.
 
 
 
3. Register on your mobile phone
Install the application on your mobile phone without running the program.
 
4. How to access other applications
External Application UsageContentResolverClass to access data in ContentProvider (CRUD operation)
Get the parser ContentResolver
 ContentResolver resolver = Context. getContentResolver ();
Access the ContentProvider associated with the Uri through resolver. insert (), delete (), update (), query ()
 
 
 
 
 
5. Uri Processing
URI indicates the data to be operated. It consists of three parts: scheme, authorites, and path.
Eg:
Content: // com. jxn. provider/person
Scheme | authorites | path
 
   1. schema: indicates that you want to access ContentProvider. Fixed to: "content ://" 
   2, Authority (host name or authorization ):Defines which ContentProvider provides the data.
3. path: path, which can be based on the business logicCustom. Eg: person, person/insert, person/insert/10, etc.
4. ID: Generally, the Placeholder "#" is used to replace the URI with the corresponding number.
"Content: // com. jxn. provider/person/#" # indicates the data id (# indicates any number)
"Content: // com. jxn. provider/person/*" * to match any text
 
 
The Android system provides two tool classes for Uri operations:UriMatcher and ContentUris 
 
   1. The UriMatcher class is used to match the Uri.The usage is as follows:
 
Step 1: register all the Uri paths you need to match, as shown below:
 
// Constant UriMatcher. NO_MATCH indicates that the return code does not match any path.
UriMatcher = new UriMatcher (UriMatcher. NO_MATCH );
// If the match () method matches the content: // com. jxn. provider. personprovider/person path, the return matching code is 1.
Matcher. addURI ("com. jxn. provider. personprovider", "person", 1); // Add a uri to be matched. If yes, a matching code is returned.
// If the match () method matches the content: // com. jxn. provider. personprovider/person/230 path, the return matching code is 2.
Matcher. addURI ("com. jxn. provider. personprovider", "person/#", 2); // # It is a wildcard
 
Step 2: Use the matcher. match (uri) method to match the input Uri. If the matching succeeds, the matching code is returned.
Switch (matcher. match (Uri. parse ("content: // com. jxn. provider. personprovider/person/10 "))){
Case 1
// Corresponding business operations
Break;
Case 2
// Corresponding business operations
Break;
Default:
// Corresponding business operations
Break;
}
 
2. The ContentUris class is used to add an ID to the path and obtain the path ID.
 
Add id: ContentUris. withAppendedId (Uri, id) to uri)
Get id: ContentUris. parseId (uri)
 
 
 
 
 
6. Monitor Data changes of content providers
 
 
1. If the visitor of ContentProvider needs to know that the data in ContentProvider has changed, call getContentResolver () when the data in ContentProvider changes (). notifyChange (uri, null) to notify the visitor registered on this URI, for example:
 
Public class PersonContentProvider extends ContentProvider {
 
Public Uri insert (Uri uri, ContentValues values ){
 
Db. insert ("person", "personid", values );
 
// Note: If the notifyChange () method is not called, the data changes in ContentProvider are unknown even if other applications have registered the ContentObserver.
 
GetContext (). getContentResolver (). policychange (uri, null );
 
}
 
}
 
 
2. If the visitor of ContentProvider needs to be notified of data changes, you must use ContentObserver to listen to the data (described in uri). When the notification of data changes is received, the system will call the onChange () method of ContentObserver:
 
GetContentResolver (). registerContentObserver (Uri. parse ("content: // com. jxn. providers. personprovider/person"), true, new PersonObserver (new Handler ()));
 
Public class PersonObserver extends ContentObserver {
 
Public PersonObserver (Handler handler ){
 
Super (handler );
 
}
 
Public void onChange (boolean selfChange ){
 
// Here you can perform corresponding business processing
 
}
 
}
 
 
 
7. Supplement
 
GetType () method: Mainly used to match the data type and return the MIME type of the data represented by the current Uri.
 
If the returned data is a single data entry: vnd. android. cursor. item
 
If multiple data entries are returned: vnd. android. cursor. dir