Service-one of the four Android Components

Source: Internet
Author: User

Service-one of the four Android Components

1. service is one of the four major components of Android, namely, the Service of Android system (not a thread, but a part of the main program). Unlike Activity, it cannot interact with users, cannot be started by yourself. You need to call Context. startService () to start and run the background. if the Service process does not end when we exit the application, it is still in the background. For example, when we play music, we may want to listen to music and do other things. When we quit playing music, if we don't need Service, we won't be able to hear the song, so now we have to use the Service. 2. service Life Cycle ① onCreate () Create Service ② onStart (Intent intent, int startId) Start Service ③ onDestroy () destroy Service ④ onBind () return an IBinder interface object to Service 3. start and Stop Service ① start: startService (Intent intent) to start the Service. At this time, the Service will call its onCreate () method (this Service has not been created), and then call the onStart () method. ② Stop: stopService (Intent intent) to stop the Service. At this time, the Service will call its onDestory () method. 4. bindService (Intent Service, ServiceConnection conn, int flags) is called to bind a service. In this case, the Service calls its onCreate () method (this Service has not been created ), then, the onBind () method is called to return an IBinder interface object of the client. (Note: If null is returned, the ServiceConnection object method will not be called.) parameter ① service: Intent object. Parameter ② conn: ServiceConnection object, which is implemented when onServiceConnected () and onServiceDisconnected () are successfully connected and disconnected. Parameter ③ flags: the method used to create a Service. BIND_AUTO_CREATE indicates that the service is automatically created when it is bound. To include the service in the compilation system, it must be in AndroidMainfest. explicitly declare the Service in xml. 2. If the workload is large and it is not the work of the UI Layer, you can choose to place it in the Service for work. 3. through the development documentation, you will find that the Service in Android is in the same thread as the host (caller), rather than a dedicated thread, which means that if your Service requires CPU-intensive operations (such: MP3 playback) or a blocking operation (such as the network), it must generate its own thread to complete this work, otherwise it will cause thread blocking. In the subclass of the Service, the IntentService class Service can be implemented as a standard, and its work has its own thread. 4. if you use a broadcast receiver to work with the Service, if the broadcast is dynamically registered, remember to call unregisterReceiver (receiver ER) when the Service is stopped ); this method is used to cancel the receiver 1) Context. startService (): the Service will go through onCreate-> onStart (if the Service is not running, android first calls onCreate () and then calls onStart (); if the Service is already running, only onStart () is called, so the onStart method of a Service may be called multiple times). When stopService is called, onDestroy is called directly. If the caller directly exits without calling stopService, the Service is always running in the background. After the Service caller starts up again, the Service can be closed through stopService. Note that Context is called multiple times. startservice () is not nested (even if the corresponding onStart () method is called), no matter how many times the same service is started, Once Context is called. stopService () or stopSelf. Note: Intent objects passed to startService () are passed to the onStart () method. The call sequence is onCreate --> onStart (which can be called multiple times) --> onDestroy. (2) Context. bindService (): the Service will go through onCreate ()-> onBind (), onBind will return to the client an IBind interface instance, IBind allows the client to call back the Service method, for example, you can obtain the Service running status or other operations. At this time, the caller (such as Activity) will be bound with the Service, and the Context will exit, and Srevice will call onUnbind-> onDestroyed to exit accordingly, the so-called binding together will survive. Note: Intent objects passed to bindService () will be passed to onBind (), Intent objects passed to unbindService () will be passed to onUnbind () method. The call sequence is onCreate --> onBind (only once, cannot be bound multiple times) --> onUnbind --> onDestory. (3) Note: during each Service enabling and disabling process, only onStart can be called multiple times (through multiple startService calls), other onCreate, onBind, onUnbind, onDestory can only be called once in a lifecycle. Another point is that I have never encountered a situation where the interaction between startService and bindService is required (I don't think there will be such a requirement), so I don't have to consider the issue of interaction, so it is not too late to consider the treatment. (4) BroadcastReceiver can only start the Service through startService, because the broadcast itself has a short life cycle and bind is meaningless.

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.