Some understanding of Android services

Combined with the information read, explain some understanding of Android services:

  1. An Android application can have multiple services. For example, music software can play music in the background and download music at the same time.

  2. An application can have multiple services, and each service can run in a separate process, depending on how the service is configured.

    In the AndroidManifest.xml file, android:processyou can control the running process of the service by assigning attributes to the service component. If the properties of the two services android:processdiffer, they will run in separate processes.

  3. If no configuration Android:processproperties are shown, the service runs by default in the application's main process. There can be multiple services in a process.

  4. There are two ways to start the service: bind the service and start the service.

  5. A process for an application process and a service (a service can have a separate process).

    • If the application process is killed, all services under the main process will be destroyed. Including bound services and start-up services.
    • If the application process is killed, even if the startup service is running under a separate process. It will also be destroyed. Because the services of independent processes still depend on the application's main process and context, when the main process is terminated, these dependencies cannot continue to exist.
  6. The application process is killed and the application process is terminated is not the same.

    • The killing of the application process means that the operating system actively terminates the application process, usually to reclaim system resources or respond to the system's memory management strategy. In this case, the termination of the application process is controlled by the operating system, and the application itself cannot prevent or intervene. When an application process is killed, all components, services, and threads related to the process will stop running, and the state and data of the application will be lost. At this point, the application needs to be restarted to resume operation.
    • Termination of the application process means that the application process exits normally or is manually closed by the user. In this case, the application terminates the process through normal lifecycle methods (such as onDestroy()) or user action (such as pressing the back key or closing the application through the task manager). In this case, the application has the opportunity to save state or perform some cleanup operations before the process terminates. When the application process is terminated, the components, services, and threads related to the process will also stop running, and the state and data of the application may also be lost.
  7. You manually use the Android device's system task manager to clean up the process is a process of killing.

  8. Killing process and application process termination will destroy all services.

  9. For start-up services: onStartCommandThe return value of the method determines what action the system should take after processing the start command.

    • START_STICKY: If the system terminates the service due to insufficient memory after execution onStartCommand(), the system will try to recreate the service and call it when the memory resources are sufficient onStartCommand(), but will not redeliver the last Intent. This means that the service will remain in the started state, but the previous Intent data will not be restored. This is useful for services that do not keep user data. (play music)
    • START_NOT_STICKYonStartCommand(): If the system terminates the service after executing due to insufficient memory, the service will remain terminated until it is explicitly started again. In this mode, the service is not recreated, nor is any saved Intent data delivered. This is suitable for services that perform one-off tasks. (calendar alarm clock reminder)
    • START_REDELIVER_INTENT: If the system terminates the service due to insufficient memory after execution onStartCommand(), the system will try to recreate the service and call it when the memory resources are sufficient onStartCommand(), and re-deliver the previous Intent. This ensures that when the service restarts, it can continue to process previously unfinished tasks and restore the previous state. (data upload)
  10. A service can be both a bound service and an initiated service. Such a service must be unbound and stopped before it can be destroyed.

Guess you like

Origin blog.csdn.net/qq_43867812/article/details/131557477