ContextWrapper.StartService(Intent) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Request that a given application service be started.
[Android.Runtime.Register("startService", "(Landroid/content/Intent;)Landroid/content/ComponentName;", "GetStartService_Landroid_content_Intent_Handler")]
public override Android.Content.ComponentName? StartService(Android.Content.Intent? service);
[<Android.Runtime.Register("startService", "(Landroid/content/Intent;)Landroid/content/ComponentName;", "GetStartService_Landroid_content_Intent_Handler")>]
override this.StartService : Android.Content.Intent -> Android.Content.ComponentName
Parameters
- service
- Intent
Identifies the service to be started. The Intent must be either fully explicit (supplying a component name) or specify a specific package name it is targetted to. 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
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. On Android 5.0 and later, an app targeting API level 21 or higher must specify a component or package; otherwise, this method throws an IllegalArgumentException. On those Android versions, earlier-targeting apps receive a warning, and a matching service may be selected. 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. The system delivers start requests by calling the target service's Service.onStartCommand(Intent, int, int) method with the supplied intent. This provides a convenient way to submit jobs to a service without having to bind and call on to its interface. A started service can be stopped with stopService(Intent) or by calling stopSelf(). Calls to startService() do not nest: one stop call removes the started state, but the service may remain running while clients are bound with BIND_AUTO_CREATE. The system may also stop background services or kill the service process. Whether a killed service restarts depends on its start mode and other conditions. This function will throw SecurityException if you do not have permission to start the given service. Note: 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 bound services for high frequency calls. Beginning with Android 8.0, apps targeting API level 26 or higher have limits on starting background services while the app is in the background. Exceptions apply. See Background Execution Limits for more details. Beginning with Android 12, apps targeting API level 31 or higher generally cannot start foreground services from the background. Exceptions apply. See Behavior changes: Apps targeting Android 12 for more details.
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.
Java documentation for android.content.ContextWrapper.startService(android.content.Intent).