Android programming implements how to start and stop service

  • 2021-01-06 00:44:18
  • OfStack

This article illustrates how Android programming can start and stop service. To share with you for your reference, as follows:

Let's start with 1 Service

You can start a service from an activity or from another application component by passing an Intent(specifying the service to start) to startService(). The Android system then calls the onStartCommand() method of service and passes Intent to it. You can never call onStartCommand() directly.

For example, one activity can be used when calling startService() with one explicit intent starting with the example service(HelloSevice) in the previous section:

Intentintent = new Intent(this, HelloService.class);

The startService() method immediately returns and the Android system calls the onStartCommand() method of service. But if service is not yet running, the system calls onCreate() first, then onStartCommand().

If service does not provide binding, the intent passed to startService() is the only way to communicate between an application component and service. However, if you want service to send back a result, the client that starts service can create an PendingIntent for broadcasting (using getBroadcast()) and pass it to service in intent. service can then broadcast the result back and forth.

Different start requests lead to different calls to onStartCommand() of service, but there is only one request to stop service (using stopSelf() or stopService()).

Stop 1 service

A "booted" service must manage its own lifetime. This means that the system will not stop or destroy this service unless it runs out of memory and service continues to run after onStartCommand() returns. So, service must either stop itself by calling stopSelf() or stop it by another component calling stopService().

Once a stop request is made via stopSelf() or stopService(), the system destroys service as quickly as possible.

However, if your service is processing multiple requests to onStartCommand() at the same time, you should not stop service after processing one request, because you may have received a new start request (stopping the first one after completion will terminate the second one). 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 start request ID(startId to onStartCommand()) to service. If service receives a new start request before you call stopSelf(int) and finds that ID is different, then service will not stop.

Note: It is very important that your application stops all of its ES90en after it completes its work. This avoids wasting system resources and consuming power. Other components can call stopService() to stop service if needed. Even if you enable binding for service, you will have to stop service yourself, even if it receives a call to onStartCommand().

Create 1 binding for Service

A binding service allows an application component to bind to it by calling bindService() to create a persistent connection (and generally does not allow a component to start it by calling startService()).

When your ES106en or other components want to interact with service or your application wants to provide functionality based on IPC to other applications, you should create a binding service.

To create a bound service, you must implement the callback method onBind() and return an IBinder, which defines the interface to communicate with service. Other application components can then call bindService() to receive this interface and start calling ES118en methods. service only lives when an application component is bound to it, so when no component is bound to it, the system kills it (you don't have to stop a bound service, unlike service started with onStartCommand()).

To create a binding service, the first thing to do is to define the interface for how the client communicates with service. This interface must be an implementation of IBinder and must be returned by the callback method onBind(). Once the client receives IBinder, it can begin to interact with service.

Multiple clients can bind to one service at a time. When a client finishes interacting with service, it calls unbindService() to unbind. As soon as no client is bound to service anymore, the system kills service.

There are many ways to implement 1 binding service and these implementations are much more difficult to understand than the "original" service.

Send notifications to the user

Once up and running, an service can notify users of events via Toast notifications or status bar alerts.

An toast notification is a message that appears on the surface of the current window and disappears after 1. When a status bar notification provides an icon with a message to the status bar, it can be predefined to perform some action (such as launching an activity).

In general, a status bar notification is the best way to notify the user that something can be done with it when some background work (such as a file download) is complete. When the user selects this notification, it can start 1 activity(for example, it can view downloaded files).

For more information on Android components, please refer to the Android Usage Summary.

I hope this article is helpful to Android program design.

Related articles: