To create a service, you must create a subclass of Service
or use one of its existing subclasses. In your implementation, you must override some callback methods that handle key aspects of the service lifecycle and provide a mechanism that allows the components to bind to the service, if appropriate. These are the most important callback methods that you should override:
创建service,你要继承Service或者使用已有的子类。
你要override一些方法。
下面是那些重要的方法
The system invokes this method by calling startService()
when another component (such as an activity) requests that the service be started. When this method executes, the service is started and can run in the background indefinitely. If you implement this, it is your responsibility to stop the service when its work is complete by calling stopSelf()
or stopService()
. If you only want to provide binding, you don't need to implement this method.
翻译:startService会调用onStartCommand
The system invokes this method by calling bindService()
when another component wants to bind with the service (such as to perform RPC). In your implementation of this method, you must provide an interface that clients use to communicate with the service by returning an IBinder
. You must always implement this method; however, if you don't want to allow binding, you should return null.
翻译:bindService会调用onBind方法
The system invokes this method to perform one-time setup procedures when the service is initially created (before it calls either onStartCommand()
or onBind()
). If the service is already running, this method is not called.
翻译:onCreate方法只调用一次
The system invokes this method when the service is no longer used and is being destroyed. Your service should implement this to clean up any resources such as threads, registered listeners, or receivers. This is the last call that the service receives.
翻译:在onDestroy你要清理资源,注册的listenter,reveiver等等。。