I. Basic concepts of Content Provider
1. ContentProvider provides a unified interface for storing and acquiring data. The contentprovide encapsulates the data. Don't care about the details of the data store. Use the form of tables to organize your data.
2. Use ContentProvider to share data between different applications.
3, Android for some of the common data provided by default ContentProvider (including audio, video, pictures and contacts, etc.).
The functions provided by the ContentProvider:
Query (), insert (), update (), delete (), GetType (), OnCreate (), and so on.
Ii. use of URIs (Uniform Resource Identifiers)
Give the system a name for each resource, such as 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 illustrate that a content provider controls these data and cannot 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 customize. Remember to keep it consistent when you use it, OK; Content://hx.android.text.myprovider/tablename "
D: Assuming that the URI contains the ID of the record that is required to get it, the corresponding data for that ID is returned. Assuming that there is no ID, it returns all; " content://hx.android.text.myprovider/tablename/# "#表示数据id
Three, the realization process of contentprovider
It is uncommon to implement contentprovider yourself. Because you may not need to exchange data with other applications. Use the built-in ContentProvider more.
1. Define a Content_uri constant that provides an identifier for the access ContentProvider.
public static final Uri Content_uri =uri.parse ("Content://com.example.codelab.transportationprovider");
Among them: content is protocol
Com.exmaple.codelab.transportationprovider is the class name. Include the full package name.
Uri.parse converts a string to a URI type.
Suppose provider includes a child table. The same definition includes the Content_uri of 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 columns. Make sure to include a _id column inside.
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 matches the URI and returns different custom-defined values according to the different URIs that are passed in to indicate the type of different resources that the URI is visiting.
Like what:
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 add additional URI matching paths. The first parameter passes in the authority string that identifies the ContentProvider.
The second argument passes in the path that needs to be matched. The # number here is a wildcard character, which represents random numbers, and can be used to match random text. The third argument must pass in a match code greater than 0. The match () method is used to return the corresponding matching code to the matching URI. For example: Smatcher.adduri ("Com.test.provider.personprovider", "person", 1); Suppose 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