Share via


Context.StartService(Intent) Method

Definition

Request that a given application service be started.

[Android.Runtime.Register("startService", "(Landroid/content/Intent;)Landroid/content/ComponentName;", "GetStartService_Landroid_content_Intent_Handler")]
public abstract Android.Content.ComponentName? StartService (Android.Content.Intent? service);
[<Android.Runtime.Register("startService", "(Landroid/content/Intent;)Landroid/content/ComponentName;", "GetStartService_Landroid_content_Intent_Handler")>]
abstract member StartService : Android.Content.Intent -> Android.Content.ComponentName

Parameters

service
Intent

Identifies the service to be started. The Intent must be fully explicit (supplying a component name). Additional values may be included in the Intent extras to supply arguments along with this specific start call.

Returns

If the service is being started or is already running, the ComponentName of the actual service that was started is returned; else if the service does not exist null is returned.

Attributes

Exceptions

Remarks

Request that a given application service be started. The Intent should either contain the complete class name of a specific service implementation to start, or a specific package name to target. If the Intent is less specified, it logs a warning about this. In this case any of the multiple matching services may be used. If this service is not already running, it will be instantiated and started (creating a process for it if needed); if it is running then it remains running.

Every call to this method will result in a corresponding call to the target service's android.app.Service#onStartCommand method, with the <var>intent</var> given here. This provides a convenient way to submit jobs to a service without having to bind and call on to its interface.

Using startService() overrides the default service lifetime that is managed by #bindService: it requires the service to remain running until #stopService is called, regardless of whether any clients are connected to it. Note that calls to startService() do not nest: no matter how many times you call startService(), a single call to #stopService will stop it.

The system attempts to keep running services around as much as possible. The only time they should be stopped is if the current foreground application is using so many resources that the service needs to be killed. If any errors happen in the service's process, it will automatically be restarted.

This function will throw SecurityException if you do not have permission to start the given service.

<div class="caution">

<strong>Note:</strong> Each call to startService() results in significant work done by the system to manage service lifecycle surrounding the processing of the intent, which can take multiple milliseconds of CPU time. Due to this cost, startService() should not be used for frequent intent delivery to a service, and only for scheduling significant work. Use #bindService bound services for high frequency calls.

Beginning with SDK Version android.os.Build.VERSION_CODES#O, apps targeting SDK Version android.os.Build.VERSION_CODES#O or higher are not allowed to start background services from the background. See

Background Execution Limits for more details.

<strong>Note:</strong> Beginning with SDK Version android.os.Build.VERSION_CODES#S, apps targeting SDK Version android.os.Build.VERSION_CODES#S or higher are not allowed to start foreground services from the background. See

Behavior changes: Apps targeting Android 12

for more details. </div>

Java documentation for android.content.Context.startService(android.content.Intent).

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

  • StopService(Intent)
  • <xref:Android.Content.Context.BindService(Android.Content.Intent%2c+Android.Content.IServiceConnection%2c+Android.Content.IServiceConnection)>