I. Sharing data using ContentProvider (content provider)
The role of ContentProvider in Android is to share data externally , which means that the data in the app can be shared with other applications via ContentProvider, and other applications can add and remove data from your app through ContentProvider.
Just like the call logs on our phone. I don't want to see a talk time. This is required to call the data in the call log . It is time to use the contentprovider data sharing.
About data sharing. In the file operation mode, data can be shared externally by specifying the mode of operation of the file for context.mode_world_readable or context.mode_world_writeable. So. Why use ContentProvider to share data? The answer is: If you use the file operation mode to share data externally, the data access will vary depending on how the data is stored. The data access can not be unified, such as: the use of sharedpreferences shared data, you need to use the sharedpreferences API to read data, using XML files to share data externally. XML parsing ability is required to read data. The advantage of using ContentProvider to share data externally is that it unifies the way data is interviewed.
Applications need to share data via ContentProvider step:
The first step requires inheriting ContentProvider and overriding the following methods:
Com.example.testcp.firstcontentprovider/users
public boolean onCreate (): This method is called after ContentProvider is created. After Android is powered on, ContentProvider is created when the other app first visits it.Public URI insert (URI uri, contentvalues values): This method is used for external applications to add data to ContentProvider.
public int Delete (URI Uri, String selection, string[] Selectionargs): This method is used for external applications to delete data from ContentProvider. public int update (URI uri, contentvalues values, String selection, string[] Selectionargs) : This method is used for external applications to update data in ContentProvider.
Public Cursor query (URI uri, string[] projection, string selection, string[] Selectionargs, String sortOrder): This method is used for external applications from Get the data in the ContentProvider.
Public String GetType (URI uri): This method is used to return the MIME type of the data represented by the current URL.
Assume that the data for the operation belongs tocollection type。 Then the MIME-type string should bevnd.android.cursor.dir/Beginning.
For example: To get the full person record the URI is Content://com.example.testcp.firstcontentprovider/users, then the MIME type string returned should be: " Vnd.android.cursor.dir/user ".
Suppose that the data to be manipulated belongs toNon-collection type data, then the MIME type string should bevnd.android.cursor.item/Beginning.
For example: Get a person record with ID 10. URI is content://com.example.testcp.firstcontentprovider/users/1. The MIME type string returned is: "Vnd.android.cursor.item/iser".
such as code:
The definition of the data type returned by the ContentProvider. Take more than one data public static final String Content_Type = "Vnd.android.crusor.dir/vnd.firstprovider.user";// The definition of the data type returned by the ContentProvider, takes a data public static final String Content_type_item = "vnd.android.cursor.item/ Vnd.firstprovider.user ";
The second step is to use <provider> to configure the ContentProvider in Androidmanifest.xml. In order to allow other applications to find the ContentProvider. ContentProvider uses authorities (hostname/domain name) to uniquely identify it, and you can think of ContentProvider as a site. The site is also providing data to the person, authorities is his domain name:
<provider android:name= "Com.example.testcp.FirstContentProvider" android:authorities= " Com.example.testcp.FirstContentProvider "> </provider>
Ii. introduction of URIs
The URI represents the data to be manipulated , and the URI consists mainly of two pieces of information:
1) ContentProvider to be operated.
2) to manipulate what data in ContentProvider, a URI consists of the following parts:
Scheme ContentProvider (content provider) Scheme: content://
The span style= "color: #ff0000" > host (Authoritis) is used to uniquely identify this contentprovider. The external caller can find it based on this identity. The
path can be used to represent the data we want to manipulate . The path should be built according to the business.
For example, the following:
To manipulate records with ID 1 in the Users table, you can build this path:/users/1
The name field to manipulate the record with ID 1 in the Users table. Users/1/name
To manipulate all the records in the person table, you can build this path:/users
Of course the data to be manipulated does not necessarily come from the database. It can also be a file, XML, or network, and other storage methods.
Suppose you want to convert a string to a URI. Ability to use the parse () method in the URI class. For example, the following:
Uri uri = uri.parse ("Content://com.example.testcp.firstcontentprovider/users")
Iii. Introduction to the use of Urimatcher class
The Android system provides two tool classes for manipulating URIs, Urimatcher and Contenturis, respectively. The Urimatcher class is used to match URIs. Its use method is as follows:
First, you need to match the URI path to all of the notes. For example, the following:
Check whether the URI is legal public static final Urimatcher urimatcher;public static final int incoming_user_collection = 1;public s tatic Final int incoming_user_single = 2;private databasehelper dh;static {//constant Urimatcher.no_match indicates mismatch no matter what Return code for path Urimatcher = new Urimatcher (urimatcher.no_match); Urimatcher.adduri (Firstprovidermetadata.author IY, "/users", incoming_user_collection); Urimatcher.adduri (Firstprovidermetadata.authoriy, "/users/#", INCOMING_ User_single); #号为通配符switch} switch (Urimatcher.match (URI)) { Case Incoming_us er_collection: return usertablemetadata.content_type; Case incoming_user_single: & nbsp return usertablemetadata.content_type_item; default: THRow new IllegalArgumentException ("Unknown uri" + uri); }
Iv. Introduction to the use of Contenturis class
The Contenturis class is used to manipulate the ID portion after the URI path, which has two useful methods:
The Withappendedid (URI, id) is used to add the ID portion of the path:
The resulting URI is: Content://com.example.testcp.firstcontentprovider/users/1uri uri = Uri.parse ("content:// Com.example.testcp.firstcontentprovider/users ") Uri Inserteduseruri = Contenturis.withappendedid (URI, 1);
The Parseid (URI) method is used to get the ID part from the path:
The result obtained is: 1Uri Inserteduseruri = Uri.parse ("CONTENT://COM.EXAMPLE.TESTCP.FIRSTCONTENTPROVIDER/USERS/1") long userId = Contenturis.parseid (Inserteduseruri);
Here the basic instructions have been completed, then we will implement the following contentprovider:
v. Use Contentresolver to manipulate the data in the ContentProvider when external applications need to add, delete, change, and query data in ContentProvider Be able to use the Contentresolver class to complete, to get the Contentresolver object. Ability to use the Getcontentresolver () method provided by the activity. The Contentresolver class provides the same four methods that are signed with the ContentProvider class: the first parameter of these methods is a URI. Represents the contentprovider to be manipulated and what data is to be manipulated.
Public URI insert (URI uri, contentvalues values): This method is used to add data to ContentProvider. public int Delete (URI Uri, String selection, string[] Selectionargs): This method is used to delete data from ContentProvider. public int update (URI uri, contentvalues values, String selection, string[] Selectionargs): This method is used to update data in ContentProvider.Public Cursor query (URI uri, string[] projection, string selection, string[] Selectionargs, String sortOrder): This method is used for the Conte Get the data in the Ntprovider.
The following is the code for the Insert data
Values.put (FirstProviderMetaData.UserTableMetaData.USER_NAME, "a"); The first number of parameters to which URI to insert the data URI uri = Getcontentresolver (). Insert (FirstProviderMetaData.UserTableMetaData.CONTENT_URI, values );
vi. Monitoring the change of data in ContentProvider
Suppose ContentProvider's visitors need to know that the data in the ContentProvider is changing and can call Getcontentresolver () when data changes occur in ContentProvider. Notifychange ( URI, NULL) to notify the caller of this URI, code such as the following:
Through the listener, the notification data has changed GetContext (). Getcontentresolver (). Notifychange (Inserteduseruri, NULL);
To achieve the effect of the above steps:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvc3rhcnl3ea==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "height=" width= "652" >
The following source code: http://download.csdn.net/detail/yangweixing10/7216371
Specific explanations of Android ContentProvider and URIs