当前位置: 代码迷 >> Android >> Android Service 中 onStartCommand()函数返回值含意
  详细解决方案

Android Service 中 onStartCommand()函数返回值含意

热度:110   发布时间:2016-04-28 00:35:20.0
Android Service 中 onStartCommand()函数返回值含义

onStartCommand()是由Android系统调用的,本质上也是调用了onStart()方法。

onStartCommand()返回值有几种:

1)START_STICKY

英文解释:

Constant to return from onStartCommand: if this service's process is killed while it is started (after returning fromonStartCommand), then leave it in the started state but don't retain this delivered intent. Later the system will try to re-create the service. Because it is in the started state, it will guarantee to callonStartCommand after creating the new service instance; if there are not any pending start commands to be delivered to the service, it will be called with a null intent object, so you must take care to check for this.

This mode makes sense for things that will be explicitly started and stopped to run for arbitrary periods of time, such as a service performing background music playback.

说明:当返回值是START_STICKY时,service只会在我们想要它结束运行的时候停止运行。即使service所在进程被kill掉了,系统也会重新创建一个新的service。

2)START_STICKY_COMPATIBILITY

compatibility version of START_STICKY that does not guarantee thatonStartCommand will be called again after being killed.

说明:START_STICKY的兼容版本,但是不保证被kill掉以后一定会重启

3)START_NOT_STICKY

Constant to return from onStartCommand: if this service's process is killed while it is started (after returning fromonStartCommand), and there are no new start intents to deliver to it, then take the service out of the started state and don't recreate until a future explicit call toContext.startService(Intent). The service will not receive aonStartCommand(Intent, int, int) call with a null Intent because it will not be re-started if there are no pending Intents to deliver.

This mode makes sense for things that want to do some work as a result of being started, but can be stopped when under memory pressure and will explicit start themselves again later to do more work. An example of such a service would be one that polls for data from a server: it could schedule an alarm to poll every N minutes by having the alarm start its service. When itsonStartCommand is called from the alarm, it schedules a new alarm for N minutes later, and spawns a thread to do its networking. If its process is killed while doing that check, the service will not be restarted until the alarm goes off.

说明:和START_STICKY作用相反,在被kil掉之后,不会重启新的service。适用于那些在系统资源(如memory)宽裕的情况下能run,在资源不宽裕(如内存不够)的情况下能stop的任务。

4)START_REDELIVER_INTENT

Constant to return from onStartCommand: if this service's process is killed while it is started (after returning fromonStartCommand), then it will be scheduled for a restart and the last delivered Intent re-delivered to it again viaonStartCommand. This Intent will remain scheduled for redelivery until the service callsstopSelf(int) with the start ID provided toonStartCommand. The service will not receive aonStartCommand(Intent, int, int) call with a null Intent because it will will only be re-started if it is not finished processing all Intents sent to it (and any such pending events will be delivered at the point of restart).



  相关解决方案