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

Source: Internet
Author: User
Tags sqlite database

First, service services:

A service is a long life cycle, without a user-interface program that can be used to develop programs such as monitoring classes.

A good example is a media player that is playing songs from a playlist. In a media player application, there should be multiple activity, allowing the user to select songs and play songs. However, there is no activity associated with the music replay because the user will of course think that the music should still be playing when navigating to other screens. In this example, the Media Player activity uses context.startservice () to start a service so that it can keep the music playing in the background. At the same time, the system will keep the service running until the end of the service run. In addition, we can also connect to a service by using the Context.bindservice () method (if the service is not running it will start it). When connected to a service, we can also communicate with it by the interface provided by the service. Take the Media Player example, we can also pause, replay and other operations.

Service usage steps are as follows

   1>继承service类   2>AndroidManifast.xml配置清单文件中<application>节点里对服务进行配置<service name=".SMSService"/>

Service does not run on its own and needs to be started by Contex.startservice () or Contex.bindservice ()

The service started by the StartService () method has no relationship with the caller, even if the caller shuts down and the service is still running to stop the service to call Context.stopservice (), at which point the system calls Ondestory (), when started with this method, The first time the service starts the system calls the service's OnCreate () –>onstart (), and if the service has started again the call will only trigger the OnStart () method

Services that are started with bindservice () are bound to the caller as long as the caller shuts down the service, and when started with this method, the first time the service starts the system calls the service's OnCreate () –>onbind (), and if the service has started again, the call will 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 ();

Contents Provider content Providers

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,

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),

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

Its benefits: Unified access to data.
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

├────browser

├────calllog

├────contacts

│├────groups

│├────people

│├────phones

│└────photos

├────images

│└────thumbnails

├────mediastore

│├────albums

│├────artists

│├────audio

│├────genres

│└────playlists

├────settings

└────video

Calllog: Address and received phone information

Contact.People.Phones: Store Phone number

Setting.system: System settings and preferences

To share data externally using content provider

1> inherits the ContentProvider class and overrides the following methods as required:

 Public Boolean onCreate();//Handling initialization Operations       /** * Insert data into content provider (override when other apps are allowed to insert data into your app) * @param uri * @param initialvalues inserted data * @return * *        PublicUriInsert(Uri Uri, contentvalues initialvalues);/** * Remove data from content providers (override when other apps delete data you apply) * @param uri * @param selection Conditional statement * @param selectionargs parameters * @return * *        Public int Delete(Uri Uri, String selection, string[] selectionargs);/** * Update data that the content provider already exists (overrides when other apps update your app's data) * @param uri * @param values updated data * @param selection Conditional statement * @param selectionargs parameter * @return  */        Public int Update(URI Uri, contentvalues values, String selection, string[] selectionargs);/** * Returns data to callers (overrides when other apps are allowed to fetch data from your app) * @param uri * @param projection column name * @param selection Conditional statement * @param selectionargs parameter * @param sortOrder sort *< c6> @return * /        PublicCursorQuery(Uri Uri, string[] projection, string selection, string[] Selectionargs, string sortOrder);/** * Returns 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 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 "* If the data for the operation is single data, then the returned type string should start with vnd.android.cursor.item/* For example, to obtain the URI for the person record with ID 10 is Content://com.brav ESTARR.PROVIDER.PERSONPROVIDER/PERSON/10, * Then the MIME type string returned should be "Vnd.android.cursor.item/person" * @param  URI * /        PublicStringGetType(URI Uri)

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:

   1.需要操作的contentprovider   2.对contentprovider中的什么数据进行操作,一个Uri格式:结构头://authorities(域名)/路径(要操作的数据,根据业务而定)          content://com.bravestarr.provider.personprovider/person/10

Description: ContentProvider's structure head has been defined by Android as content://

Authorities is used to uniquely identify this ContentProvider program, and external callers can find him based on this

The path represents the data we want to manipulate, and the path is built according to the business. The path format is as follows:

   要操作person表行号为10的记录,可以这样构建/person/10   要操作person表的所有记录,可以这样构建/person

2> in Androidmanifest.xml with the ContentProvider configuration registration (content provider register it itself like the site registered domain name), ContentProvider use Authoritie (originally authorized, can be understood as the domain name ) as a unique identifier for other applications to find

<application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name" >        <!-- authorities属性命名建议:公司名.provider.SomeProvider-->        <provider android:name=".PersonProvider" android:authorities="com.bravestarr.provider.personprovider"/>         ...</application>

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

Related Article

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.