Android Service 中 onStartCommand()函數傳回值含義,androiddialog傳回值

來源:互聯網
上載者:User

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).



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.