[Android] Use of Service

1. The service is related to multithreading, asynchronous, AsyncTask

  1. The screenshots and code in this article are from "First Line of Code"

2. Service

1. Create a Service class

  1. Create a service, pay attention to other programs to access and enable the service. Inherit the Service class
  2. Implement the onBinder method, the only abstract method in Service, which must be implemented in subclasses
    Insert picture description here
  3. Life cycle and processing logic
    1) After testing the StartService method, if the service exists, the onStartCommand() method will be executed, that is, the service will run when it starts. If the service does not exist, onCreate (first creation) will be executed, so if you want the service to execute as soon as it starts, you can prevent it from onSC.
    Insert picture description here

2. Register the xml manifest file (all four components are required)

Insert picture description here

3. Start and stop services in Activity

  1. Intent declaration + corresponding method to transfer the established intent
  2. You can stopSelf() method yourself in the service
    Insert picture description here

3. Activity and service contact [use binder]

1. In service

  1. Define a class to inherit Binder. This class provides methods to control the Service required by the activity. This class needs to declare an instance object in the service
  2. The onBinder() method returns the object
    Insert picture description here

2. Add two buttons to the AC layout file for binding and unbinding

Insert picture description here

3. In Activity [Need to hold a connection to control the binder]

  1. Define a class to implement ServiceConnection or use this anonymous class notation
    1) (Java is different from Kotlin), Java seems to declare an object plus {} to be an anonymous class. . . Rewrite the two methods to correspond to the callbacks of binding and unbinding. 所以多个活动绑定返回的都是相同的Binder,因此既然都要绑定所以service的binder没必要做成static
    Insert picture description here
    2) The Servicebinder returned after the connection is assigned to our binder, and we can control it in the Activity.

  2. Also declare an instance object of the Binder class in Service
    Insert picture description here

  3. Monitor two button click events (the purpose of binderService is to obtain a persistent connection to a service)
    Bold style

  4. Note
    1) When the bindService() method is called, and the unbindService() method is called, the onDestroy() method will also be executed.
    2) Mixed start. For a service, both the startService() method and the bindService() method are called. In this case, how can the service be destroyed? According to the mechanism of the Android system, a service will always be running as long as it is started or bound, and the above two conditions must be met before the service can be destroyed. Therefore, in this case, the stopService() and unbindService() methods must be called at the same time , and the onDestroy() method will be executed.

  5. Use front desk service to keep alive
    Insert picture description here

4. The service stops automatically after execution

1. Start the thread, execute the method, execute the call stopSelf()

Insert picture description here

2. A better way IntentService (set thread start and automatic stop in one)

  1. New class inherits IntetntService
  2. onHandleIntent() is already running in a child thread
    Insert picture description here
  3. intent mode unchanged
    Insert picture description here
  4. Register in manifest file
    Insert picture description here

Guess you like

Origin blog.csdn.net/qq_38304672/article/details/113049889