Service development is relatively simple, as follows: Step 1: Inherit the Service class Public class SMSService extends Service {} Step 2: add Row configuration: <service android: name = ". SMSService"/> The service cannot run on its own. You must call Context. startService () or Start the service using the Context. bindService () method. Both methods can start the Service, They are used differently. Use startService () to enable services, callers and services The service is still running even if the caller exits. BindService () method When the service is enabled, the caller and the service are bound together. Once the caller exits, the service is terminated, It has the characteristics of "not seeking to live at the same time, but having to die at the same time. If you plan to start the service using the Context. startService () method, when the service is not created, The system first calls the onCreate () method of the service and then calls the onStart () method. If you call The service has been created before the startService () method. The startService () method is not called multiple times. The onStart () method is called multiple times. Use startService () Only the Context. stopService () method can be called to end the service. The onDestroy () method is called. If you plan to start the service using the Context. bindService () method, when the service is not created, The system first calls the onCreate () method of the service and then calls the onBind () method. Call The user and the service are bound together. When the caller exits, the system will first call the onUnbind () of the service () Method, and then call the onDestroy () method. If the service already exists before the bindService () method is called The bindService () method is called multiple times and does not cause multiple service creation and binding (that is The onCreate () and onBind () methods are not called multiple times ). If the caller wants You can call the unbindService () method to unbind a bound service. The onUnbind () --> onDestroy () method of the system call service. Common lifecycle callback methods for services are as follows: OnCreate () This method is called only once when the service is created. The number of startService () or bindService () methods, the service is created only once. OnDestroy () is called when the service is terminated. Life cycle method related to starting a service using Context. startService () OnStart () calls back this method only when the Context. startService () method is used to start the service. This method is called when the service starts running. The startService () method is called multiple times, although not multiple times. Create a service, but the onStart () method is called multiple times. The lifecycle method related to starting a service using the Context. bindService () method OnBind () calls back this method only when the Context. bindService () method is used to start the service. This method is called when the caller is bound to the service. When the caller is bound to the service, it is called multiple times. The Context. bindService () method does not cause this method to be called multiple times. OnUnbind () is called back only when the Context. bindService () method is used to start the service. Method. This method is called when the caller and the service are unbound. |