当前位置:网站首页>Two startup methods and differences of Service

Two startup methods and differences of Service

2022-08-11 07:51:00 Is A small A

Service startup method: startService() and bindService()

startService()

public abstract void startService(Intent service)

Parameters

Service name of the service program to be started

Methods

This method will call the onCreate() and onStartCommand() methods in the Service to start a background Service, and directly call the onDestroy() method when the Service is destroyed

bindService()

public abstract boolean bindService(Intent service,ServiceConnection conn,int flags)

Parameters

service is the name that defines the service program to bind

conn is the interface program responsible for receiving information when the service program starts and stops

flags are options for setting binding, which can be 0, BIND_AUTO_CREATE, BIND_DEBUG_UNBIND, BIND_NOT_FOREGROUND, BIND_ABOVE_CLIENT, BIND_ALLOW_OOM_MANAGEMENT or BIND_WAIVE_PRIORITY

Methods

This method starts the Service through the bindService() method, and its life cycle is controlled by its bound object.A Service can be bound to multiple objects at the same time. When no object is bound to the Service, the Service will be destroyed by the system

The difference between the two startup methods

1. startService just starts the Service, and the components that start it (such as Activity) are not associated with the Service. Only when the Service calls stopSelf or other components call stopService, the service will be terminated

2. The bindService method starts the Service, and the caller is bound to the service. Once the caller quits, the service will be terminated.

3. The bindService method starts the service. Other components can obtain the proxy object of the Service and interact with the Service through the callback, and the two parties are also bound. When the initiator is destroyed, the Service will also automatically perform the unBind operation.The Service will only be destroyed when all bindings have been unBinded

原网站

版权声明
本文为[Is A small A]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/223/202208110644322462.html