Android four basic components (2) service services and content provider contents providers

Source: Internet
Author: User
<span id="Label3"></p> <p><p><strong>first, Service Repair:</strong></p></p><p><p>A service This is a long life cycle and there is no real user interface program that can be used to develop programs such as monitoring Categories.</p></p><p><p>The media player that plays the song in the Table. In the application of a media player. There should be multiple activity, allowing users to select songs and play Songs. However. Music Replay This function does not have the corresponding activity, because the user certainly will feel when navigates to other screens the music should still be playing. In this Example. Media Player the activity uses Context.startservice () to start a service, allowing music to be played back in the Background. At the same time, the system will keep the service up and Executing. Until the end of the service Execution. In addition, we are able to connect to a service by using the Context.bindservice () method (assuming that the service has not been executed to start it). When connected to a service, we are able to communicate with it through the interface provided by the SERVICE. Take the media player example, we are also able to pause, replay and other Operations.</p></p><p><p>Service usage procedures such as the following</p></p><pre><pre><code> 1>继承service类 2>AndroidManifast.xml配置清单文件里<application>节点里对服务进行配置<service name=".SMSService"/></code></pre></pre><p><p>The service cannot be executed on its own and needs to be started by Contex.startservice () or Contex.bindservice ()</p></p><p><p>The service started by the StartService () method has no relationship with the caller, even if the caller shuts down and the service is still executing to call Context.stopservice () to stop the service, at which point the system calls Ondestory () and when started with this method, The first time the service starts the system calls the Service's OnCreate () –>onstart (), Assuming that the service has started again, the call will only trigger the OnStart () method</p></p><p><p>The service started with Bindservice () is bound to the caller, only the caller closes the service and terminates, when started with this method, the first time the service starts the system calls the Service's OnCreate () –>onbind (), Assuming that the service has started again call no longer trigger these 2 methods , when the caller exits, the system invokes the Service's Onunbind () –>ondestory (), and the system calls Onunbind () –>ondestory () in order to actively unbind the binding Contex.unbindservice ();</p></p><p><p><strong>Contents Provider content Providers</strong></p></p><p><p>The Android platform provides content provider to make the specified data set for an application available to other Applications.</p></p><p><p>This data can be stored in a file system, in a SQLite database, or in any other reasonable way,</p></p><p><p>Other applications can obtain or deposit data from the content provider through the Contentresolver class (see Contentprovideraccessapp sample). (equivalent to outsourcing a shell in the application),</p></p><p><p>There is only a need for content providers to share data between multiple Applications.</p></p><p><p>For example, Address Book data is used by multiple applications and must be stored in a content provider</p></p><p><p>Its advantages: unified data access Method.<br>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.</p></p><p><p>See: http://developer.android.com/reference/android/provider/package-summary.html</p></p><p><p>├────browser</p></p><p><p>├────calllog</p></p><p><p>├────contacts</p></p><p><p>│├────groups</p></p><p><p>│├────people</p></p><p><p>│├────phones</p></p><p><p>│└────photos</p></p><p><p>├────images</p></p><p><p>│└────thumbnails</p></p><p><p>├────mediastore</p></p><p><p>│├────albums</p></p><p><p>│├────artists</p></p><p><p>│├────audio</p></p><p><p>│├────genres</p></p><p><p>│└────playlists</p></p><p><p>├────settings</p></p><p><p>└────video</p></p><p><p>Calllog: address and received phone information</p></p><p><p>Contact.People.Phones: Store Phone number</p></p><p><p>Setting.system: System settings and preferences</p></p><p><p>To share data externally using content provider</p></p><p><p>1> inherits the ContentProvider class and overrides the following methods according to the Requirements:</p></p><pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">Boolean</span></span> <span class="hljs-title"><span class="hljs-title">onCreate</span></span>();<span class="hljs-comment"><span class="hljs-comment">//handling Initialization Operations</span></span> <span class="hljs-javadoc"><span class="hljs-javadoc">/** * Insert data to content provider (override when other apps insert data into your APP) *<span class="hljs-javadoctag"> @param</span> uri *<span class="hljs-javadoctag"> @param</span> initialvalues inserted data *<span class="hljs-javadoctag"> @return</span> *</span> *</span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span>Uri<span class="hljs-title"><span class="hljs-title">Insert</span></span>(uri uri, contentvalues initialvalues);<span class="hljs-javadoc"><span class="hljs-javadoc">/** * Delete data from content providers (override if other apps delete your App's Data) *<span class="hljs-javadoctag"> @param</span> uri *<span class="hljs-javadoctag"> @param</span> selection Conditional statement *<span class="hljs-javadoctag"> @param</span> selectionargs number *<span class="hljs-javadoctag"> @return</span> *</span> /</span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">int</span></span> <span class="hljs-title"><span class="hljs-title">Delete</span></span>(uri uri, String selection, string[] selectionargs);<span class="hljs-javadoc"><span class="hljs-javadoc">/** * Update the data that the content provider already exists (override when other apps update your App's Data) *<span class="hljs-javadoctag"> @param</span> uri *<span class="hljs-javadoctag"> @param</span> values updated data *<span class="hljs-javadoctag"> @param</span> selection Conditional statement *<span class="hljs-javadoctag"> @param</span> Selectionargs *<span class="hljs-javadoctag"> @return</span> </span> * * *</span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">int</span></span> <span class="hljs-title"><span class="hljs-title">Update</span></span>(uri uri, contentvalues values, String selection, string[] selectionargs);<span class="hljs-javadoc"><span class="hljs-javadoc">/** * Returns data to the caller (overrides when other apps get data from your APP) *<span class="hljs-javadoctag"> @param</span> uri *<span class="hljs-javadoctag"> @param</span> projection column name *<span class="hljs-javadoctag"> @param</span> selection Conditional statement *<span class="hljs-javadoctag"> @param</span> Selectionargs *<span class="hljs-javadoctag"> @param</span> sortOrder sort *< c6> @return *</span> /</span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span>Cursor<span class="hljs-title"><span class="hljs-title">Query</span></span>(uri uri, string[] projection, string selection, string[] selectionargs, string sortOrder);<span class="hljs-javadoc"><span class="hljs-javadoc">/** * Returns the MIME type of the data represented by the current URI * Assuming that the data for the operation is of the collection type (multiple data), then the returned type string should start with vnd.android.cursor.dir/* for example, to get all The URI for the person record is content://com.bravestarr.provider.personprovider/person, * then the MIME type string returned should be "vnd.android.cursor.dir /person "* Assuming that the data for the operation is single data, 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.brav estarr.provider.personprovider/person/10, * Then the MIME type string returned should be "vnd.android.cursor.item/person" *<span class="hljs-javadoctag"> @param URI * /</span></span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span>String<span class="hljs-title"><span class="hljs-title">GetType</span></span>(uri Uri)</code></pre><p><p>The URI parameters in these methods are parsed and processed accordingly, and the URI represents the data to be manipulated, including two pieces of information:</p></p><pre><pre><code> 1.须要操作的contentprovider 2.对contentprovider中的什么数据进行操作,一个Uri格式:结构头://authorities(域名)/路径(要操作的数据,依据业务而定) content://com.bravestarr.provider.personprovider/person/10</code></pre></pre><p><p>Description: Contentprovider's structure Head has been defined by Android as content://</p></p><p><p>Authorities is used to uniquely identify this contentprovider program, and external callers can find him based on this</p></p><p><p>The path represents the data we want to manipulate, and the path is built according to the BUSINESS. the path format is as Follows:</p></p><pre><pre><code> 要操作person表行号为10的记录,能够这样构建/person/10 要操作person表的全部记录,能够这样构建/person</code></pre></pre><p><p>2> in Androidmanifest.xml using the ContentProvider to configure the Register (the content provider It itself is like a site registration domain name), ContentProvider adopted authoritie (originally authorized, can be understood as the domain name ) as a unique identifier for other applications to find</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs xml"><span class="hljs-tag"><<span class="hljs-title">application</span> <span class="hljs-attribute">android:icon</span>=<span class="hljs-value">"@drawable/ic_launcher"</span> <span class="hljs-attribute">android:label</span>=<span class="hljs-value">"@string/app_name"</span> ></span> <span class="hljs-comment"><!-- authorities属性命名建议:公司名.provider.SomeProvider--></span> <span class="hljs-tag"><<span class="hljs-title">provider</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">".PersonProvider"</span> <span class="hljs-attribute">android:authorities</span>=<span class="hljs-value">"com.bravestarr.provider.personprovider"</span>/></span> ...<span class="hljs-tag"></<span class="hljs-title">application</span>></span></code></pre></pre> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article Bo master original Article. blog, not reproduced without Consent.</p></p> <p><p> Service services for Android four basic components (2) and content provider contents 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.