Android Service 中 onStartCommand()函數傳回值含義,androiddialog傳回值
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).