Android Learning Summary--content Provider

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-size: 16px;">Original Address: http://www.cnblogs.com/bravestarrhu/archive/2012/05/02/2479461.html</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;"><strong>Content Provider</strong> <strong>content Providers:</strong></span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">The Android platform provides content provider to make the specified data set for an application available to other Applications. This data can be stored in a file system, in a SQLite database, or in any other reasonable way,</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">Other applications can obtain or deposit data from the content provider through the Contentresolver class (see Contentprovideraccessapp example). (equivalent to outsourcing a shell in the application),</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">You need content providers only if you need to share data among multiple Applications. For example, Address Book data is used by multiple applications and must be stored in a content provider</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">Its benefits: unified access to Data.</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">The content providers that come with the Android system (top-level representations of database names, Non-top-level are table names) are described in the Android.provider Java package in the SDK Documentation. See: http://developer.android.com/reference/android/provider/package-summary.html</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">├────browser</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">├────calllog</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">├────contacts</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">│├────groups</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">│├────people</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">│├────phones</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">│└────photos</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">├────images</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">│└────thumbnails</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">├────mediastore</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">│├────albums</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">│├────artists</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">│├────audio</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">│├────genres</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">│└────playlists</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">├────settings</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">└────video</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">Calllog: address and received phone information</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">Contact.People.Phones: Store Phone number</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">Setting.system: System settings and preferences</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">To share data externally using content provider</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">1> inherits the ContentProvider class and overrides the following methods as Required:</span></p></p><pre><span style="font-size: 14px;"><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Boolean</span></span>OnCreate ();<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Handling Initialization Operations</span></span> <span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* Insert data into content provider (override when other apps are allowed to insert data into your APP) *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">URI *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">initialvalues Data Inserted *</span></span><span style="color: #808080;"><span style="color: #808080;">@return</span></span> <span style="color: #008000;"><span style="color: #008000;">*/</span></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 initialvalues); </span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* Delete data from content providers (overrides when other apps are allowed to delete data from your APP) *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">URI *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">selection Conditional statement *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">Selectionargs Parameters *</span></span><span style="color: #808080;"><span style="color: #808080;">@return</span></span> <span style="color: #008000;"><span style="color: #008000;">*/</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: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* Update the data that the content provider already exists (override if other apps update your App's Data) *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">URI *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">values updated data *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">selection Conditional statement *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">Selectionargs Parameters *</span></span><span style="color: #808080;"><span style="color: #808080;">@return</span></span> <span style="color: #008000;"><span style="color: #008000;">*/</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, string[] selectionargs); </span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* Return data to callers (override when other apps are allowed to fetch data from your APP) *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">URI *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">Projection Column name *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">selection Conditional statement *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">Selectionargs Parameters *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">SortOrder Sort *</span></span><span style="color: #808080;"><span style="color: #808080;">@return</span></span> <span style="color: #008000;"><span style="color: #008000;">*/</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, string[] selectionargs, string Sorto rder); </span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* Used to return the MIME type of the data represented by the current URI * If the Operation's data is of the collection type (multiple data), then the returned type string should start with vnd.android.cursor.dir/* for example to get all per The URI of the son record is content://com.bravestarr.provider.personprovider/person, * then the MIME type string returned should be "vnd.android.cursor.dir/ Person "* If the data for the operation is single data, then the returned type string should start with vnd.android.cursor.item/* for example to get the ID 10 of the person record the URI is Content://com.brave starr.provider.personprovider/person/10, * Then the MIME type string returned should be "vnd.android.cursor.item/person" *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">URI</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span>String GetType (uri Uri)</span></pre><p align="left"><p align="left"><span style="font-size: 16px;">The URI parameters in these methods, obtained after parsing and then do the corresponding processing, the URI represents the data to be manipulated, contains two parts of information:</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">1. ContentProvider to be operated on</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">2. operation of what data in contentprovider, a URI format: structure header://authorities (domain name)/path (data to be manipulated, depending on business)</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">Content://com.bravestarr.provider.personprovider/person/10</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">Description: Contentprovider's structure Head has been defined by Android as content://</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">Authorities is used to uniquely identify this contentprovider program, and external callers can find him based on this</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">The path represents the data we want to manipulate, and the path is built according to the BUSINESS. the path format is as Follows:</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">To manipulate records with a person table row number of 10, you can BUILD/PERSON/10</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">To manipulate all records of the person table, you can Build/person</span></p></p><p align="left"><p align="left"><span style="font-size: 16px;">2> use <provider> to register the ContentProvider in Androidmanifest.xml (content provider registers itself like a website registered domain name), ContentProvider uses Authoritie (originally authorized, can be understood as the domain Name) as a unique identification, convenient for other applications to find</span></p></p><pre><span style="font-size: 14px;"><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Application</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:icon</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "@drawable/ic_launcher"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:label</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "@string/app_name"</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #008000;"><span style="color: #008000;"><!--</span></span><span style="color: #008000;"><span style="color: #008000;">authorities property naming recommendation: company Name. Provider.someprovider</span></span><span style="color: #008000;"><span style="color: #008000;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">provider</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:name</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">=". Personprovider "</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">android:authorities</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "com.bravestarr.provider.personprovider"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">/></span></span><span style="color: #000000;"><span style="color: #000000;"> ...</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Application</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span></span></pre><p><p></p></p><p><p>Android Learning Summary--content Provider</p></p></span>

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.