Android start stop service
Started Services with Xamarin.Android
Is this page helpful?
Please rate your experience
Yes
No
Any additional feedback?
Feedback will be sent to Microsoft: By pressing the submit button, your feedback will be used to improve Microsoft products and services. Privacy policy. Show
Submit
Thank you. In this articleStarted Services OverviewStarted services typically perform a unit of work without providing any direct feedback or results to the client. An example of a unit of work is a service that uploads a file to a server. The client will make a request to a service to upload a file from the device to a website. The service will quietly upload the file (even if the app has no Activities in the foreground), and terminate itself when the upload is finished. It is important to realize that a started service will run on the UI thread of an application. This means that if a service is to perform work that will block the UI thread, it must create and dispose of threads as necessary. Unlike a bound service, there is no communication channel between a "pure" started service and its clients. This means that a started service will implement some different lifecycle methods than a bound service. The following list highlights the common lifecycle methods in a started service:
The important method for a started service is the OnStartCommand method. It will be invoked each time the service receives a request to do some work. The following code snippet is an example of OnStartCommand: public override StartCommandResult OnStartCommand (Android.Content.Intent intent, StartCommandFlags flags, int startId) { // This method executes on the main thread of the application. Log.Debug ("DemoService", "DemoService started"); ... return StartCommandResult.Sticky; }The first parameter is an Intent object containing the meta-data about the work to perform. The second parameter contains a StartCommandFlags value that provides some information about the method call. This parameter has one of two possible values:
Finally, the third parameter is an integer value that is unique to the application that identifies the request. It is possible that multiple callers may invoke the same service object. This value is used to associate a request to stop a service with a given request to start a service. It will be discussed in more detail in the section Stopping the Service. The value StartCommandResult is returned by the service as a suggestion to Android on what to do if the service is killed due to resource constraints. There are three possible values for StartCommandResult:
There is a fourth value for StartCommandResult StartCommandResult.ContinuationMask. This value is returned by OnStartCommand and it describes how Android will continue the service it has killed. This value isn't typically used to start a service. The key lifecycle events of a started service are shown in this diagram: Stopping the ServiceA started service will keep running indefinitely; Android will keep the service running as long as there are sufficient system resources. Either the client must stop the service, or the service may stop itself when it is done its work. There are two ways to stop a service:
Using startId to Stop a ServiceMultiple callers can request that a service be started. If there is an outstanding start request, the service can use the startId that is passed into OnStartCommand to prevent the service from being stopped prematurely. The startId will correspond to the latest call to StartService, and will be incremented each time it is called. Therefore, if a subsequent request to StartService has not yet resulted in a call to OnStartCommand, the service can call StopSelfResult, passing it the latest value of startId it has received (instead of simply calling StopSelf). If a call to StartService has not yet resulted in a corresponding call to OnStartCommand, the system will not stop the service, because the startId used in the StopSelf call will not correspond to the latest StartService call. Related Links
|