Service Details of four Android Components
This internship has been around for more than 10 days. Today, I will sort out the Android notes for my study. The so-called good memory is not as bad as a pen, today we are talking about service components. The service can be used in multiple applications. For example, when the user starts other activities during multimedia playback, the program must continue playing the video in the background, for example, detecting file changes on the SD card, or record the changes in your geographic information location in the background. In short, the service is always hidden in the back. The Service runs in the background for a period of time and does not interact with the application components. Each Service must be declared in manifest through <service>. It can be started through contect. startservice and contect. bindserverice. Like other application components, the Service runs in the main process. This means that if the service requires a lot of time-consuming or congested operations, it needs to be implemented in its subthread. 1. The two service modes (startService ()/bindService () are not completely separated). The service cannot run on its own. You must use Contex. startService () or Contex. the bindService () Startup service is started by using the startService () method. It does not matter to the caller. Even if the caller closes the service, the Context must be called to stop the service. stopService (). At this time, the system will call onDestory (). When this method is used to start a service, the system first calls onCreate () --> onStart () of the service when the service is started (), if the service has been started and called again, it will only trigger the onStart () method to bind the service started with bindService () to the caller. Once the caller closes the service, it will terminate. When this method is used to start, when the service is started for the first time, the system calls the onCreate () --> onBind () of the service. If the service has been started and called again, the two methods will not be triggered. When the caller exits, the system will Call the onUnbind () --> onDestory () of the service. To manually unbind the service, use Contex. unbindService (), the system calls onUnbind () --> onDestory () in sequence; 2. Use context in the life cycle (as shown in. startService () starts the Service and will experience: context. startService ()-> onCreate ()-> onStart ()-> Service running context. stopService () |-> onDestroy ()-> Service stop if the Service is not running, android calls onCreate () and then 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 used, onDestroy is used directly. If the caller directly exits without calling stopService, the Service will always run in the background. After the Service caller starts up again, the Service can be closed through stopService. Therefore, the life cycle for calling startService is onCreate --> onStart (which can be called multiple times) --> onDestroy uses context. bindService () starts the Service and experiences: context. bindService ()-> onCreate ()-> onBind ()-> Service running onUnbind ()-> onDestroy ()-> Service stop onBind will return an IBind interface instance to the client, IBind allows the client to call back the Service method, such as obtaining 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-> onDestroy to exit accordingly. Therefore, the life cycle of calling bindService is onCreate --> onBind (only once, cannot be bound multiple times) --> onUnbind --> onDestory. 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.