If a component starts the service by calling startService()
(which results in a call to onStartCommand()
), the service continues to run until it stops itself with stopSelf()
or another component stops it by calling stopService()
.
翻译:startService 要 stopSelf或者stopService才能终结
If a component calls bindService()
to create the service and onStartCommand()
is not called, the service runs only as long as the component is bound to it. After the service is unbound from all of its clients, the system destroys it.
翻译:bindService 要组件全部解绑后才会终结
-----------------------------------------------------------------------------------------------------------
The Android system stops a service only when memory is low and it must recover system resources for the activity that has user focus.
低内存的时候会停止service
If the service is bound to an activity that has user focus, it's less likely to be killed;
bound到有焦点的activity的service很少被杀
if the service is declared to run in the foreground, it's rarely killed.
前台service很少被杀
If the service is started and is long-running, the system lowers its position in the list of background tasks over time, and the service becomes highly susceptible to killing—if your service is started, you must design it to gracefully handle restarts by the system.
后台service随着时间推移变得更加可能被杀
If the system kills your service, it restarts it as soon as resources become available, but this also depends on the value that you return from onStartCommand()
. For more information about when the system might destroy a service, see the Processes and Threading document.
service被杀后会重启,但是取决于onStartCommand的返回值。
In the following sections, you'll see how you can create the startService()
and bindService()
service methods, as well as how to use them from other application components.
下面讲startService和bindService这两个方法