Service concept and purpose: A service is an application component that can perform long-running operations in the background and does not provide a user interface. Generally, a service is used to perform time-consuming operations, or perform operations in the background that do not provide user interaction interfaces, such as downloading and playing music. Service lifecycle: the lifecycle of an Android Service is not as complex as that of an Activity. It only inherits three methods: onCreate (), onStart (), and onDestroy, when we start the Service for the first time, we call the onCreate () and onStart () Methods successively. when the Service is stopped, the onDestroy () method is executed. Note that, if the Service has been started, when we start the Service again, we will not execute the onCreate () method, but directly execute the onStart () method. For details, refer to the following instance. Communication between Service and Activity: the data at the backend of the Service must be displayed on the front-end Activity, because when the Service is started, the system restarts a new process, this involves communication between different processes (AIDL), which I have not described much. When we want to get the started Service instance, we can use the bindService and onBindService methods, they run the IBinder () and onUnbind () methods in the Service respectively. One thing to mention here: in the method of rewriting the service by inheriting the subclass of the service, in addition to an onStart () method, there is also an onStartCommand () method related to onStartCommand () method: During Android development, the onStartCommand (Intent, int, int) method of the Service object is called every time startService (Intent) is called, then, perform some processing in the onStartCommand method. Then we noticed that this function has an int return value. This article briefly describes the function of the int return value. From the official Android documentation, we know that onStartCommand has four return values: START_STICKY: if the service process is killed, the service State will be retained to the start state, but the intent object for delivery will not be retained. Then the system will try to re-create the service. Because the service status is starting, the onStartCommand (Intent, int, int) method will be called after the service is created. If no startup command is passed to service during this period, the Intent parameter is null. START_NOT_STICKY: "non-viscous ". When this return value is used, if the service is kill abnormally after onStartCommand is executed, the system will set it to started and the system will not automatically restart the service, the startService (Intent intent) method is called again ;. START_REDELIVER_INTENT: Re-transmits Intent. When this return value is used, if the service is kill abnormally after onStartCommand is executed, the system automatically restarts the service and passes in the Intent value. START_STICKY_COMPATIBILITY: compatible version of START_STICKY, but it is not guaranteed that the service will be restarted after it is killed. With the Service class, how can we start it? There are two methods: • Context. startService () • Context. bindService () 1. call the startService () method anywhere in the same application to start the Service. Then the system calls back the onCreate () and onStart () Methods of the Service class. In this way, the started Service will always run in the background until the Context. stopService () or selfStop () method is called. In addition, if a Service has been started and other code tries to call the startService () method, onCreate () is not executed, but onStart () is re-executed (). 2. Another bindService () method means to bind the Service to the customer class that calls the Service. If the customer class called is destroyed, the Service is also destroyed. One advantage of using this method is that after the bindService () method is executed, the Service calls back the onBind () method mentioned above. You can return a class that implements the IBind interface from here, the class can communicate with the Service by operating on the client, for example, obtaining the Service running status or other operations. If the Service is not running, the onCreate () method will be used to start the Service without calling onStart (). The difference is: the caller of startService () has no connection with the Service. Even if the caller exits, the service still runs, and the caller of bindService () is associated with the Service. Once the caller exits, the service is terminated immediately. Www.2cto.com here we use an instance (started using the startService () method) to explain the service declaration cycle and usage: First, write a class that inherits the Service base class and override the methods in it, call startService () and stopService () in the Activity to start and stop the service. Run interface: project directory structure: ExampleService. java [html] package com. service. activity; import android. app. service; import android. content. intent; import android. OS. IBinder; import android. util. log; public class ExampleService extends Service {private static final String TAG = "ExampleService"; @ Override public void onCreate () {Log. I (TAG, "ExampleService-onCreate"); super. onCreate () ;}@ Override public void onStart (Intent intent, int startId) {Log. I (TAG, "ExampleService-onStart"); super. onStart (intent, startId) ;}@ Override public int onStartCommand (Intent intent, int flags, int startId) {// execute Operation Log such as file download or playback. I (TAG, "ExampleService-onStartCommand");/** the returned status has three values: * 1 and START_STICKY. When a service process is killed at runtime, the system sets it to the started state, but does not save the Intent object passed by it. Then, the system tries to re-create the service. * 2. START_NOT_STICKY: when a service process is killed at runtime and no new Intent object is passed, The system sets it to the started state, * but the system does not re-create the service until the startService (Intent intent) method is called again; * 3. START_REDELIVER_INTENT: when a service process is killed at run time, it will be automatically created after a period of time, and the last passed Intent object will be passed over again. */Return super. onStartCommand (intent, flags, startId) ;}@ Override public IBinder onBind (Intent intent) {Log. I (TAG, "ExampleService-onBind"); return null ;}@ Override public void onDestroy () {Log. I (TAG, "ExampleService-onDestroy"); super. onDestroy () ;}} MainActivity. java [html] package com. service. activity; import android. app. activity; import android. content. intent; import android. OS. bundle; im Port android. view. view; import android. view. view. onClickListener; import android. widget. button; public class MainActivity extends Activity implements OnClickListener {private static final String TAG = "MainActivity"; // log output flag private Button btnStartService; private Button btnStopService; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); btnStartService = (Button) findViewById (R. id. btnStartService); btnStopService = (Button) findViewById (R. id. btnStopService); btnStartService. setOnClickListener (this); btnStopService. setOnClickListener (this);} // Click Event Processing listener @ Override public void onClick (View v) {Intent intent = new Intent (MainActivity. this, ExampleService. class); switch (v. getId () {case R. id. btnStartService: startServic E (intent); break; case R. id. btnStopService: stopService (intent); break; default: break ;}}finally in AndroidManifest. declare the service in xml, which is at the same level as the Activity and is written in the Application Tag: <service android: name = ". exampleService "/> after the service is created and run, after clicking" Start service "(start service for the first time), we can view the log output from the LogCat console as follows: At this time, we click" return ", activity is killed, but our Service is still Running. You can view Setting --> Application --> Running Service, as shown below: return to our Activity and click Start Service again, the console output log is as follows: onCrea If the te () method is not called, it is not created again. Then, click STOP Service. The output log is as follows: