Android service learning notes

Source: Internet
Author: User
Tags call back

 

1. What is service?

The service is running in the backgroundCode.It can run in its own process,It can also run in other applicationsProgramContext of the process (Context)., Which depends on your own needs. Other components canBind to a service(Service) Above,Call through a remote process (RPC) To callThis method. For example, in the Media Player service, when you exit the media selection user interface, you still want to play the music again.
(Service) To ensure that the music continues to play when the user interface is disabled.

 

it matches activity is similar, but it cannot be run by itself. You need to use a activity or other context object to call, context. startservice () and context. bindservice () .

two types of startup service is different. Here, it is worth noting that if you go to service oncreate or onstart for some time-consuming tasks, it is best to service starts a thread, because service is run in the main thread, UI operate or block other tasks in the main thread.

When needServiceWhat about it? For example, when playing multimedia, the user starts otherActivityAt this time, the program will continue playing in the background, such as detectionSDChanges in files on the card, or changes in the location of your geographic information in the background.

2. How to Use Service

1. The first method is to callContext. startservice ()Start and callContext. stopservice ()End,Startservice ()Parameters can be passedService.


2. The second method is to callContext. bindservice ()Start and callContext. unbindservice ()End, you can also useServiceconnectionAccessService.

The two can be used in combination, for example, I can firstStartserviceAgainBindservice.

3. Service Lifecycle

Startservice After, even if you call Startservice The process is finished, Service Still exists until a process calls Stopservice , Or Service Suicide ( Stopself () ). Bindservice After, Service Just call Bindservice The process is both dead, that is, when you call Bindservice The process is dead, so it Bind Of Service It must also be ended. Of course, it can also be called during this period. Unbindservice Let Service End.

 

For example, youStartserviceNow, IBindserviceSo only youStopserviceAndUnbindserviceNow, thisServiceWill be ended.

 

Android The system will try to retain the processes started or bound to the service. If the service is running Oncreate (), onstart (), Or Ondestroy () When these methods are executed , Then the main process will become a foreground Process , To ensure that the Code is not stopped. If the service has already started , Then its main process is less important than all visible processes but higher than invisible processes. , Only a few processes are visible to users. , So as long as the memory is not particularly low , This service will not stop . . If multiple clients are bound to a service , As long as one of the clients is visible to the user , That is, the service is visible.

 

IfServiceIf notAndroidCallOncreate ()Then callOnstart ()IfServiceIf it is already running, onlyOnstart (), SoServiceOfOnstartThe method may be called multiple times.

 

StopserviceDirectlyOndestroyIf the caller directly exits without callingStopserviceThen,ServiceIt will always run in the background. TheServiceAfter the caller starts upStopserviceCloseService.

 

ThereforeStartserviceThe lifecycle is as follows:Oncreate --> onstart (Multiple calls) --> Ondestroy

 

UseContext. bindservice ()StartServiceExperience:

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

Onunbind ()-> ondestroy ()-> service stop

 

onbind Returns a ibind interface instance, ibind allows the client to call back the service, for example, service running status or other operations. In this case, the caller ( context , for example, activity ) it is bound to service , context quit, srevice is called onunbind-> ondestroy exits.

 

ThereforeBindserviceThe lifecycle is as follows:Oncreate --> onbind (Only once, cannot be bound multiple times) --> Onunbind --> ondestory.

 

ThereforeServiceDuring each enabling and disabling process, onlyOnstartCan be called multiple times(Multiple timesStartserviceCall), OtherOncreate,Onbind,Onunbind,OndestoryIt can be called only once in a lifecycle.

 


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.