Share via


Service.OnStartCommand(Intent, StartCommandFlags, Int32) Method

Definition

Called by the system every time a client explicitly starts the service by calling android.content.Context#startService, providing the arguments it supplied and a unique integer token representing the start request.

[Android.Runtime.Register("onStartCommand", "(Landroid/content/Intent;II)I", "GetOnStartCommand_Landroid_content_Intent_IIHandler")]
public virtual Android.App.StartCommandResult OnStartCommand (Android.Content.Intent? intent, Android.App.StartCommandFlags flags, int startId);
[<Android.Runtime.Register("onStartCommand", "(Landroid/content/Intent;II)I", "GetOnStartCommand_Landroid_content_Intent_IIHandler")>]
abstract member OnStartCommand : Android.Content.Intent * Android.App.StartCommandFlags * int -> Android.App.StartCommandResult
override this.OnStartCommand : Android.Content.Intent * Android.App.StartCommandFlags * int -> Android.App.StartCommandResult

Parameters

intent
Intent

The Intent supplied to android.content.Context#startService, 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
StartCommandFlags

Additional data about this start request.

startId
Int32

A unique integer representing this specific request to start. Use with #stopSelfResult(int).

Returns

The return value indicates what semantics the system should use for the service's current started state. It may be one of the constants associated with the #START_CONTINUATION_MASK bits.

Attributes

Remarks

Called by the system every time a client explicitly starts the service by calling android.content.Context#startService, providing the arguments it supplied and a unique integer token representing the start request. Do not call this method directly.

For backwards compatibility, the default implementation calls #onStart and returns either #START_STICKY or #START_STICKY_COMPATIBILITY.

<p class="caution">Note that the system calls this on your service's main thread. A service's main thread is the same thread where UI operations take place for Activities running in the same process. You should always avoid stalling the main thread's event loop. When doing long-running operations, network calls, or heavy disk I/O, you should kick off a new thread, or use android.os.AsyncTask.

Java documentation for android.app.Service.onStartCommand(android.content.Intent, int, int).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

See also