This article describes the method of starting and stopping service by Android programming. Share it for your reference, as follows:
Start a Service
You can start a service to startService() by passing an Intent (specifying the service to be started) from an activity or from other applications. The Android system then calls the service's onStartCommand() method and passes the Intent to it. (You can never call onStartCommand() directly.)
For example, an activity can use an explicit intent to start the service(HelloSevice) in the previous example when calling startService():
Intentintent = new Intent(this, ); startService(intent);
The startService() method will be returned immediately and the Android system will call the service's onStartCommand() method. However, if the service is not running yet, the system will first call onCreate() and then call onStartCommand().
If the service does not provide binding function, the intent passed to startService() is the only way to communicate between the application component and the service. However, if you want the service to post a result back, the client that starts the service can create a PendingIntent for broadcasting (using getBroadcast()) and then put it in the intent to pass it to the service, and the service can then use the broadcast to send the result back and forth.
Different startup requests result in different calls to the service's onStartCommand(), but there is only one request to stop the service (using stopSelf() or stopService()).
Stop a service
A "started" service must manage its own lifespan. This means that the system will not stop or destroy this service unless there is insufficient memory and the service will continue to run after onStartCommand() returns. Therefore, the service must call stopSelf() to stop itself or stopService() by another component to stop it.
Once a stop request is issued through stopSelf() or stopService(), the system will destroy the service as quickly as possible.
However, if your service handles multiple requests to onStartCommand() at the same time, you should not stop the service after processing one request, because you may have received a new startup request (stop after the first one is completed and the second one will be eliminated). To avoid this problem, you can use stopSelf(int) to ensure that your stop request corresponds to your most recent start request. That is, when you call stopSelf(int), you pass the ID of the start request (startId passed to onStartCommand()) to the service. If the service receives a new start request before you call stopSelf(int), and finds that the ID is different, the service will not stop.
Note: It is very important for your application to stop all its services after it is done. This can avoid wasting system resources and power consumption. If necessary, other components can call stopService() to stop the service. Even if you enable binding for the service, you have to stop the service yourself, even if it receives a call to onStartCommand().
Create a bound Service
A bound service allows the application's components to bind it by calling bindService() to create a long-standing connection (and generally does not allow the component to call startService() to start it).
When your activity or other components want to interact with the service or your application wants to provide functionality to other applications based on IPC, you should create a bound service.
To create a bound service, you must implement the callback method onBind(), and return an IBinder in it, which defines the interface to communicate with the service. Other application components can then call bindService() to receive this interface and start calling the service method. The service is only alive when an application component is bound to it, so when no component is bound to it, the system will kill it (you don't need to stop a bound service, which is different from a service started with onStartCommand()).
To create a bound service, the first thing to do is to define the interface for how the client communicates with the service. This interface must be an implementation of IBinder and must be returned by the callback method onBind(). Once the client receives the IBinder, it can begin interacting with the service.
Multiple clients can be bound to a service together. When a client completes interaction with the service, it calls unbindService() to unbind. Once no clients are bound to the service, the system will kill the service.
There are many ways to implement a bound service and these implementations are much harder to understand than the "start" service.
Send notifications to the user
Once running, a service can notify users of some events through Toast notifications or status bars.
A toast notification is a message that appears on the surface of the current window and disappears after a while. When a status bar notification provides an icon with a message to the status bar, you can first set it to perform some actions (such as starting an activity).
Usually, a status bar notification is the best way to notify users of action when some background work (such as a file download is completed) is completed. When the user selects this notification, it can start an activity (such as viewing the downloaded file).
For more information about Android components, please visit the special topic of this site:Summary of the usage of basic Android components》
I hope this article will be helpful to everyone's Android programming design.