ContentProvider as one of the four components, there must be a reason for it? So why quote ContentProvider? Let's look at an example first.
People in life with mobile phones, such as you press 360 mobile phone butler, of which 360 mobile phone Butler has a message, call records, contact backup function. The messages in the phone, the contacts are all stored in the database.
For example, contact information is stored in the contacts2.db. Since the data is stored in the database, the 360 mobile phone Butler has to get the data. However, Contac2.db does not have permissions on other users. What about the 360 how to get the phone contact information?
The Android system then introduced ContentProvider. Since third-party programs cannot directly manipulate data in the database, third parties can manipulate the data through the interfaces provided by the ContentProvider. So ContentProvider's role is to realize the sharing of data between different applications. for example, the contact program, want to share their own data to others, but can not let others directly operate, so the contact program through the ContentProvider to expose their own data. But if a third-party program wants to get that data, it has to go through contentresolver to get that data.
So now that the access data has to go through contentresolver to access the data exposed by ContentProvider, how does Contentresolver know where the data is stored? The premise is that ContentProvider must tell the data where you go to fetch it. This time and introduce another person concept URL.
When you lift a URL, the first thing you might think about is the URL on the Internet (Uniform Resource Locator). This URL is a URL on the Internet, and the URL in the Android system represents an address. But there is a difference between the two:
For example, Baidu homepage of the image URL as an example:
Compare the URLs in our Android:
Now that you know what the URL is, here are some common ways to learn about URLs:
1: Convert a string to a URL
Uri uri = uri.parse ("CONTENGT://COM.DEMO.CONTENTPROVIDER/USER/1");
2: Add ID part to URL path
Uri uri = uri.parse ("Content://com.demo.contentprovider/user"); Uri Resuri = Contenturis.withappendedid (URI, 1);/* The URI after the build is: CONTENT://COM.DEMO.CONTENTPROVIDER/USER/1 */
3: Is it legal to match this URL?
/* The default value of the constructor method is to return-1, which means that the mismatch returns -1*/urimatcher Matcher = new Urimatcher (-1);/* If the match is: content://com.demo.contentprovider/ User, the return is 1*/matcher.adduri ("Com.demo.contentprovider", "User", 1);/* If the match is: content://com.demo.contentprovider/ USER/2, return to 2*/matcher.adduri ("Com.demo.contentprovider", "user/#", 2);//#代表通配符
Today, the knowledge about ContentProvider and the URL is here.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Four components Learning ContentProvider one