Service details of Android four components

Source: Internet
Author: User

  Service details of Android four components

to this internship has been more than 10 days, today to organize the study of Android notes. The so-called good memory is not as bad as written, today say service components.

    service can be used in multi-occasions applications, such as multimedia playback when the user started other activity this time the program to continue to play in the background, such as the detection of changes in the file on the SD card, or in the background to record your geographic information changes and so on, in short, the service is always hidden behind.

service is running at an indefinite time in the background, the component is not applied to interact with the user. Each service must be declared by <service> in manifest. Can be started by Contect.startservice and Contect.bindserverice.

 Service, like other application components, runs in the main thread of the process. This means that if the service requires a lot of time-consuming or blocking operations, it needs to be implemented in its child threads.

1. Two modes of service (StartService ()/bindservice () are not completely separated) 

service cannot run on its own and needs to be passed Contex.startservice () or contex.bindservice () Start the service

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 using 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 ();

 2, life cycle (as shown)

650) this.width=650; "style=" width:604px;height:544px; "title=" 2012050219083160.jpg "src=" http://s3.51cto.com/wyfs02/ M00/75/1f/wkiol1yzgxnyx4biaafvpb1b_zc453.jpg "width=" 556 "height=" 540 "alt=" wkiol1yzgxnyx4biaafvpb1b_zc453.jpg "/

Using context.startservice () to start a service is going to go through:
Context.startservice ()->oncreate ()->onstart ()->service running
Context.stopservice () | ->ondestroy ()->service stop
If the service is not running, Android calls OnCreate () and then calls OnStart (), and if the service is already running, only OnStart () is called, so a service's OnStart method may be repeated multiple times.
StopService Direct OnDestroy, if the caller is directly exiting without calling StopService, the service will always run in the background. The service can be shut down by StopService after the caller has started up again.
So the life cycle of calling StartService is: onCreate---OnStart (can be called multiple times)
--OnDestroy

  start Service experience:

Context.bindservice ()->oncreate ()->onbind ()->service Running

Onunbind (), OnDestroy ()->service stop

Onbind will return a Ibind interface instance to the client, Ibind the method that allows the client to callback the service, such as getting the service running state or other operations. This time the caller (context, for example, activity) is bound together with the service, the context exits, Srevice will call Onunbind->ondestroy exit accordingly.

So the life cycle of calling Bindservice is: onCreate--Onbind (one time, not multiple bindings)--onunbind--and ondestory.

During every service turn-off, only onstart can be called multiple times (via multiple StartService calls), other Oncreate,onbind,onunbind, Ondestory can only be called once in a life cycle.

Service details of Android four components

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.