<span id="Label3"></p><p><p>By learning from previous articles, knowing that Contentresolver is using ContentProvider to get other data that is shared with the application, the interface between Contentresolver and ContentProvider should be similar. Where ContentProvider is responsible for organizing the application data, providing data to other applications, Contentresolver is responsible for obtaining the data provided by contentprovider, modifying/adding/deleting update data, etc.;</p></p><p><p><strong>How does ContentProvider provide data to the outside world?</strong></p></p><p><p>Android provides contentprovider, a program can completely expose its own data by implementing a ContentProvider abstraction interface, and Contentproviders exposes the data in a similar way to a table in a database, This means that ContentProvider is like a "database". Then the external access to the data they provide should be the same as the operation of fetching data from the database, but the use of URIs to represent the "database" that the outside world needs to access. As to how to identify the "database" from the uri, this is what Android needs to do on the ground floor, not in Detail. A brief analysis of the interface that ContentProvider provides data manipulation to the outside world:</p></p><p><p>Query (Uri, string[], string, string[], String)</p></p><p><p>Insert (Uri, contentvalues)</p></p><p><p>Update (Uri, contentvalues, String, String[])</p></p><p><p>Delete (Uri, String, String[])</p></p><p><p>These operations are essentially the same as the operations of the database, where it is not detailed to say that the URI is where special instructions are required:</p></p><p><p>The d part of the URI may contain a _id, which should appear in the SQL statement, and can appear in a special way, which requires us to pay extra attention to this particular information when we provide the Data. The Android SDK recommends that you include an ID in the provided data table field, and the integer PRIMARY KEY autoincrement identifies this ID field when you create the table</p></p> <blockquote> <blockquote> <blockquote> </blockquote> </blockquote> </blockquote><p><p><strong>How does the ContentProvider organize the data?</strong></p></p><p><p>Organization data mainly includes: storing data, reading data, exposing data in the way of Database. Data storage needs to be based on the design needs, choose the appropriate storage structure, the preferred database, of course, you can choose other local files, or even data on the Network. Data reading, exposing data as a database this requires that, regardless of how the data is stored, the data must eventually be accessed in the form of Data.</p></p><p><p>There are probably 2 more questions that need to be paid attention to.</p></p><p><p>When was the ContentProvider created, and who created it? Do you need to launch this application to access the data shared by an application? This issue is not explicitly stated in the Android sdk, but from the point of view of data sharing, ContentProvider should be Android is created when the system is started, otherwise there is no data sharing. This requires explicit definition of elements in Androidmanifest.xml.</p></p><p><p>Could there be more than one program accessing a contentprovider through contentresolver, will it lead to "dirty data" like a database? This problem requires the synchronization of database access, especially the synchronization of data Writes. When defining ContentProvider in androidmanifest.xml, you need to consider the value of the element multiprocess attribute, and on the other hand Android provides notifychange in Contentresolver ( ) interface that notifies other contentobserver when the data changes, this place should use the observer pattern, and there should be some interfaces similar to Register,unregister in the Contentresolver.</p></p><p><p>How does Android implement data sharing between applications? We talked about outside programs that can access the data provided by ContentProvider through the Contentresolver interface, and today we talk about how to create our own contentprovider to implement data sharing between Applications.</p></p><p><p>An application can create its own data, which is private to the application, is more invisible to the outside world, does not know how the data is stored, whether it is using a database or a file, or is obtained online, none of which matters, It is important for the outside world to interact with the data in this program through a set of standards and a unified interface, such as add (insert), delete, query, Modify (update).</p></p><p><p>Android provides us with ContentProvider to share data, a program to define its own contentprovider if it wants other programs to manipulate its own Data. It is then registered in the androidmanifest.xml, and other application can manipulate the data of the previous program by obtaining contentresolver through a Uri.</p></p><p><p>The data of the phone book in Android is shared by contentprovider, and there are many shared URIs existing in the System. We can use Contentresolver to manipulate data from different tables using uris, such as Contacts.People.CONTENT_URI.</p></p><p><p>There are two ways to query content provider: the Contentresolver query () and the Managedquery () of the Activity object, both of which receive the same parameters, all of which are the cursor objects, and the only difference is Use the Managedquery method to allow activity to manage the life cycle of the Cursor.</p></p><p><p>The managed cursor invokes its own Deactivate method when the activity enters a paused state, and calls its own Requery method to re-query the resulting cursor object when the activity returns to the running State. If an unassigned cursor object wants to be managed by activity, it can be implemented by invoking the Startmanagingcursor method of the Activity.</p></p><p><p><strong>ContentProvider what is a uri?</strong></p></p><p><p>Divide it into A,b,c,d 4 parts:</p></p><p><p>A: Standard prefix used to describe a content provider control of these data, cannot be changed; "content://"</p></p><p><p>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: it is generally the package that defines the Contentprovider. The name of the class; Content://com.android.calendar "(uri of System Calendar)</p></p><p><p>C: path, a URI under the item, just like a website, the main page contains a lot of small pages. Here the popular is the database you want to manipulate the name of the table, or you can also define your own, remember to use the time to maintain consistency is ok; "content://com.android.calendar/calendars"</p></p><p><p>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;</p></p><p><p>"content://com.android.calendar/calendars/#" #表示数据id (#代表任意数字) "content://com.android.calendar/calendars/*" * to match any text</p></p><p><p><strong>Urimatcher: used to match the uri, it uses the Following:</strong></p></p><p><p>1. First put the URI path you need to match all to the Registration.</p></p><p><p>1. The constant Urimatcher.no_match represents a return code (-1) that does not match any path. Urimatcher urimatcher = new Urimatcher (urimatcher.no_match);</p></p><p><p>2. If the match () method Content://com.android.calendar/calendars path, The return match code is 1 Urimatcher.adduri ("content:// Com.android.calendar "," Calendars ", 1);</p></p><p><p>3. Add the URI that needs to be matched, if match will return match code//if match () method matches</p></p><p><p>CONTENT://COM.ANDROID.CALENDAR/CALENDARS/23 path, return match code is 2 Urimatcher.adduri ("content://com.android.calendar", " calendars/# ", 2);</p></p><p><p>2. After registering a URI that needs to be matched, you can use the Urimatcher.match (uri) method to match the input uri, and if the match returns a matching code, the match code is the third parameter passed in by calling the Adduri () method, assuming that the match</p></p><p><p>Content://com.android.calendar/calendars path, The matching code returned is 1.</p></p><p><p>Contenturis: used to get the ID part after the URI path, it has two more practical methods:</p></p><p><p>Withappendedid (uri, Id) is used to add the ID portion of the path</p></p><p><p>The Parseid (uri) method is used to get the ID part from the path</p></p><p><p>Here is an example of a simple explanation:</p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>String uri_authority = "com.calendarwidget.provider"<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>String uri_path2 = "recordset/#";<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">just padding, no effect</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>Uri Content_uri = Uri.parse ("content://"<span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span><span style="color: #000000;"><span style="color: #000000;">Urimatcher smatcher; </span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>All_event_records = 0<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span>Smatcher =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Urimatcher (urimatcher.no_match); </span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #000000;"><span style="color: #000000;">Smatcher.adduri (uri_authority, uri_path, all_event_records); </span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #000000;"><span style="color: #000000;">Smatcher.adduri (uri_authority, uri_path2, all_event_records); </span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Boolean</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate ()</span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(mcontext = =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) </span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span>Mcontext =<span style="color: #000000;"><span style="color: #000000;">GetContext (); </span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #000000;"><span style="color: #000000;"> } </span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">Cursor Query (uri uri, string[] projection, String selection,</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Match Code</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Match =<span style="color: #000000;"><span style="color: #000000;">Smatcher.match (uri); </span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Switch</span></span><span style="color: #000000;"><span style="color: #000000;">(match)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">all_event_records:</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span>Cur = loadallcalendarevent (<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">default</span></span><span style="color: #000000;"><span style="color: #000000;">: </span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">cur; </span></span><span style="color: #008080;"><span style="color: #008080;"></span> at</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">matrixcursor loadallcalendarevent (calendarprovider calendarprovider)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Matrixcursor MC =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Matrixcursor (calendarconstants.projection); </span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Calendarcursor =<span style="color: #000000;"><span style="color: #000000;">Calendarprovider</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>. getcontentresolver (). Query ("content://com.android.calendar/calendars"<span style="color: #000000;"><span style="color: #000000;">, </span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> while</span><span style="color: #000000;"><span style="color: #000000;">(calendarcursor.movetonext ())</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO</span></span><span style="color: #008080;"><span style="color: #008080;"></span> in</span> <span style="color: #000000;"><span style="color: #000000;">..... </span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> to</span> <span style="color: #000000;"><span style="color: #000000;">Mc.addrow (rowobject); </span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span>}<span style="color: #0000ff;"><span style="color: #0000ff;">finally</span></span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">Calendarcursor.close (); </span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #008080;"><span style="color: #008080;"></span> *</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">String getType (uri Uri)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> $</span> <span style="color: #008080;"><span style="color: #008080;">Panax Notoginseng</span></span> <span style="color: #000000;"><span style="color: #000000;">} </span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">URI Insert (uri uri, contentvalues values)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> $</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Delete (uri uri, String selection, string[] Selectionargs)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> $</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">update (uri uri, contentvalues values, String selection,</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;">} </span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;">Wuyi</span></span> <span style="color: #000000;"><span style="color: #000000;">} </span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Calendarprovider<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">ContentProvider</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>String Uri_path = "RecordSet";<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">just padding, no effect</span></span><span style="color: #008080;"><span style="color: #008080;"></span> wu</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">{ </span></span><span style="color: #008080;"><span style="color: #008080;"></span> about</span> <span style="color: #000000;"><span style="color: #000000;">} </span></span><span style="color: #008080;"><span style="color: #008080;"></span> $</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Context mcontext; </span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">{ </span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">} </span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">string[] selectionargs, String sortOrder)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #000000;"><span style="color: #000000;">} </span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;">{ </span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Cursor Calendarcursor =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #008080;"><span style="color: #008080;"></span> $</span> <span style="color: #000000;"><span style="color: #000000;">. GetContext ()</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span>,<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">, </span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span>,<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span>); /<span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;">} </span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">mc; </span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">} </span></span><span style="color: #008080;"><span style="color: #008080;"></span> in</span> <span style="color: #000000;"><span style="color: #000000;">{ </span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #008080;"><span style="color: #008080;"></span> about</span> <span style="color: #000000;"><span style="color: #000000;">{ </span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;">} </span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;">string[] Selectionargs)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>0;</pre><p><p>It's easy to see that the action and category are easy to match, and how the data for our URI matches, and that the system will adjust the gettype in your contentprovider, get the relevant return value and match it to the above string. of course, The return of GetType you need to define YOURSELF.<br><br>But in the program you can also know the type of data, directly matched: Intent.settype (type);</p></p><p><p></p></p><p><p>Transferred from: http://www.2cto.com/kf/201207/144022.html</p></p><p><p>Android Contentresolver Detailed</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service