Service_android one of the four components

Source: Internet
Author: User

1.Service is what service is one of Android's four components, namely Android system services (not a thread, is a part of the main program), unlike the activity, it can not interact with the user, can not start their own, Need to call Context.startservice () to start, run the background, if we quit the app, the service process does not end, it is still in the background row. For example, when we play music, we may want to listen to music while doing some other things, when we quit playing music applications, if not the service, we can not hear the song, so this time we have to use the service. 2.Service life cycle ①oncreate () Create Service②onstart (Intent Intent, int startid) Start Service③ondestroy () Destroy Service④onbind () Returns a IBinder interface object to service 3. Start and stop Service① boot: startservice (Intent Intent) to start the service, when the service calls its own oncreate () Method (the service was not created), and then call the OnStart () method. ② stop: StopService (Intent Intent) to stop the service, the service calls its own ondestory () method. 4. Bind service Call Bindservice (Intent service, serviceconnection conn, int flags) to bind a service, then the service calls its own oncreate () Method (the service is not created), and then calls the Onbind () method to return a IBinder interface object to the client. (Note: If the method that returns the Null,serviceconnection object is not called), the parameter ①service:intent the object. The parameter ②conn:serviceconnection object that implements its onserviceconnected () and onservicedisconnected () processing when the connection succeeds and disconnects. Parameter ③flags:service is created in a way that is typically used service.bind_auto_create to automatically create a binding when the service is registered, in order to include services in the compilation system, The service must be explicitly declared in Androidmainfest.xml.2. A large amount of computing is not the work of the UI layer, you can choose to be placed in the service to work. 3. By developing the documentation you will find that the service in Android is in the same thread as the host (caller), not a thread, which means that if your service is CPU intensive (e.g. MP3 playback) or blocking operations (such as a network), It must produce its own thread to do the work, or it will cause the thread to block. Within the subclass of the service, the Intentservice class service can be implemented as a standard, and its work has its own thread. 4. If the broadcast receiver is used in conjunction with the service, the broadcast should remember to call Unregisterreceiver (receiver) when the service is stopped, if it is dynamically registered; This method to unregister the receiver 1) context.startservice (): Service will experience OnCreate and OnStart (if the service is not running, Android calls OnCreate first () Then call OnStart (); If the service is already running, only call OnStart (), so the OnStart method of a service may be repeated multiple times); StopService directly OnDestroy, If the caller exits directly without calling StopService, the service will run in the background. The service can be shut down by StopService after the caller has started up again. Note that multiple calls to Context.startservice () are not nested (even if the corresponding OnStart () method is called), so no matter how many times the same service is started, once the Context.stopservice () or stopself is called ( ), he will be stopped. Supplemental Note: The intent object passed to StartService () is passed to the OnStart () method. The call order is: onCreate---OnStart (can be called multiple times)--OnDestroy. (2) Context.bindservice (): Service will go through OnCreate (), Onbind (), Onbind will return to the client a Ibind interface instance, ibind allow the client to callback the service method, such as getting the service running state or other operations. This time the caller (context, for example, activity) is bound to the service and the context exits, and the SrEvice will call Onunbind--ondestroyed corresponding exit, so-called binding together on the survival of the total. Supplemental Note: The intent object passed to Bindservice () is passed to Onbind (), and the intent object passed to Unbindservice () is passed to the Onunbind () method. The call order is: onCreate---Onbind (one time, not multiple bindings)--Onunbind----ondestory. (3) Note: In the service every time the turn off process, only onstart can be called multiple times (through multiple StartService calls), other Oncreate,onbind,onunbind, Ondestory can only be called once in a life cycle. Also, I have not encountered the need for StartService and bindservice interaction (I think there is no such requirement), so there is no need to consider the problem of interaction, the treatment is not too late to consider. (4) Broadcastreceiver can only start the service through StartService, because the broadcast itself has a short life cycle, bind words have no meaning

Service_android one of the 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.