最終更新:2016-09-29 (木) 18:22:42 (2757d)  

Service.onStartCommand
Top / Service.onStartCommand

public int onStartCommand (Intent intent, int flags, int startId)

概要

  • このメソッドはアクティビティなどの別のコンポーネントが、Context.startService() を呼び出してサービスの開始を要求したときにシステムにより呼び出されます。一度このメソッドが実行されるとサービスが開始され、無期限にバックグラウンドで実行されます。
  • これを実装した場合、作業が完了したら stopSelf() または stopService() を呼び出してサービスを停止させる責任は開発者にあります
  • バインドのみを提供したい場合は、このメソッドを実装する必要はありません

パラメータ

  • intent - The Intent supplied to startService(Intent), as given. This may be null if the service is being restarted after its process has gone away, and it had previously returned anything except START_STICKY_COMPATIBILITY.
  • flags - Additional data about this start request. Currently either 0, START_FLAG_REDELIVERY, or START_FLAG_RETRY.
  • startId - A unique integer representing this specific request to start. Use with stopSelfResult?(int).

呼び出し元

戻り値

  • 戻り値説明
    START_NOT_STICKYサービスが強制終了した場合、サービスは再起動しない
    START_STICKYサービスが強制終了した場合、サービスは再起動するonStartCommand()が再度呼び出され、Intentにnullが渡される
    START_REDELIVER_INTENT?サービスが強制終了した場合、サービスは再起動するonStartCommand()が再度呼び出され、Intentに直前のものが渡される

一覧

  • START_NOT_STICKYif this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), 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 to Context.startService(Intent).
    START_REDELIVER_INTENT?if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then it will be scheduled for a restart and the last delivered Intent re-delivered to it again via onStartCommand(Intent, int, int).
    START_STICKYif this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then leave it in the started state but don't retain this delivered intent.
    START_STICKY_COMPATIBILITYcompatibility version of START_STICKY that does not guarantee that onStartCommand(Intent, int, int) will be called again after being killed.

メモ

  • 再起動するまでんにSTART_STICKYは5秒位、START_REDELIVER_INTENT?は40秒位かかるらしい(初回)
  • 2回目以降の再起動は2^回数*10秒後にスケジュールされる

関連

参考