I. Basic concepts of Content Provider
1. ContentProvider provides a unified interface for storing and acquiring data. Contentprovide encapsulates the data without worrying about the details of the data store. Use the form of tables to organize your data.
2. Using ContentProvider, you can share data between different applications.
3. Android provides default contentprovider for some common data (including audio, video, images, contacts, etc.).
The functions provided by the ContentProvider:
Query (), insert (), update (), delete (), GetType (), OnCreate (), and so on.
Ii. How URIs (Uniform Resource identifiers) are used
Give a name to every resource in the system, say a call log.
1, each contentprovider has a public URI, which is used to represent the data provided by this contentprovider.
2. The contentprovider provided by Android are stored in the Android.provider package. Divide it into a,b,c,d 4 parts:
A: Standard prefix, used to describe a content provider control of these data, can not be changed; content://"
The identity of the B:uri, which defines which content provider provides the data. For third-party applications, to guarantee the uniqueness of the URI identity, it must be a complete, lowercase class name. This identity is described in the authorities attribute of the element: typically the package that defines the ContentProvider. The name of the class; Content://hx.android.text.myprovider "
C: path, do not know is not the path, popular speaking is the database you want to manipulate the name of the table, or you can also define yourself, remember to use the time to maintain consistency is OK; Content://hx.android.text.myprovider/tablename "
D: If the URI contains an ID that represents the record that needs to be fetched, then the data corresponding to that ID is returned, and if there is no ID, it returns all; " content://hx.android.text.myprovider/tablename/# "#表示数据id
Three, the realization process of contentprovider
It is not common to implement contentprovider yourself, because you may not need to exchange data with other applications. Using built-in ContentProvider is much more.
1. Define a Content_uri constant that provides access to the ContentProvider identifier.
public static final Uri Content_uri =uri.parse ("Content://com.example.codelab.transportationprovider");
Where: content is an agreement
Com.exmaple.codelab.transportationprovider is the class name that contains the full package name.
Uri.parse converts a string to a URI type.
If provider contains a child table, it also defines the Content_uri that contains the Word table.
Content://com.example.codelab.transportationprovider/train
Content://com.example.codelab.transportationprovider/air/domestic
Content://com.example.codelab.transportationprovider/air/international
Then define the column to ensure that it contains a _id column.
2, define a class, inherit ContentProvider.
public class Firstcontentprovider extends ContentProvider
First introduce the urimatcher used by ContentProvider. An important function of the urimatcher is the match (Uri Uri). This function can match the URI to return different custom shaping values based on the different URIs that are passed in to indicate the type of different resources that the URI accesses.
For example:
public static final Urimatcher Urimatcher;
static {
Urimatcher = new Urimatcher (urimatcher.no_match);
Urimatcher.adduri (book.authority, "item", Book.item);
Urimatcher.adduri (book.authority, "item/#", book.item_id);
}
The static field of the Urimatcher type here is the class used to match the URI passed in to the ContentProvider. The matching code passed in its construction method is the value returned when the match () method matches the root path, which can represent a matching root path or an incoming-1 for a number greater than 0, which means that the constant urimatcher.no_match represents an unmatched root path. The Adduri () method is used to increase the other URI matching path, and the first parameter passes in the authority string that identifies the ContentProvider. The second argument passes in the path that needs to be matched, where the # number is a wildcard, which means matching any number, and you can match any text with *. The third parameter must pass in a match code that is greater than 0 and is used by the match () method to return the corresponding matching code for the matching URI. For example: Smatcher.adduri ("Com.test.provider.personprovider", "person", 1), or if the match () method matches content:// Com.test.provider.personprovider/person path with a return match code of 1.
3, implement Query,insert,update,delete,gettype and OnCreate method.
4, in the androidmanifest.xml of the Declaration.
Android Learning 19: ContentProvider Preliminary