Note: The methods startForeground and stopForeground were introduced in Android 2. In order to run your service in the foreground on older versions of the platform, you must use the previous setForeground method—see the startForeground documentation for information about how to provide backward compatibility.
For more information about notifications, see Creating Status Bar Notifications. The lifecycle of a service is much simpler than that of an activity. However, it's even more important that you pay close attention to how your service is created and destroyed, because a service can run in the background without the user being aware.
The service lifecycle—from when it's created to when it's destroyed—can follow two different paths:. The service is created when another component calls startService. The service then runs indefinitely and must stop itself by calling stopSelf. Another component can also stop the service by calling stopService.
When the service is stopped, the system destroys it.. The service is created when another component a client calls bindService. The client then communicates with the service through an IBinder interface.
The client can close the connection by calling unbindService. Multiple clients can bind to the same service and when all of them unbind, the system destroys the service. The service does not need to stop itself. These two paths are not entirely separate. That is, you can bind to a service that was already started with startService. For example, a background music service could be started by calling startService with an Intent that identifies the music to play. Later, possibly when the user wants to exercise some control over the player or get information about the current song, an activity can bind to the service by calling bindService.
In cases like this, stopService or stopSelf does not actually stop the service until all clients unbind. Like an activity, a service has lifecycle callback methods that you can implement to monitor changes in the service's state and perform work at the appropriate times.
The following skeleton service demonstrates each of the lifecycle methods:. Note: Unlike the activity lifecycle callback methods, you are not required to call the superclass implementation of these callback methods. Figure 2. The service lifecycle. The diagram on the left shows the lifecycle when the service is created with startService and the diagram on the right shows the lifecycle when the service is created with bindService.
The onCreate and onDestroy methods are called for all services, whether they're created by startService or bindService. If the service is started, the active lifetime ends the same time that the entire lifetime ends the service is still active even after onStartCommand returns. If the service is bound, the active lifetime ends when onUnbind returns. Note: Although a started service is stopped by a call to either stopSelf or stopService , there is not a respective callback for the service there's no onStop callback.
So, unless the service is bound to a client, the system destroys it when the service is stopped— onDestroy is the only callback received. Figure 2 illustrates the typical callback methods for a service. Although the figure separates services that are created by startService from those created by bindService , keep in mind that any service, no matter how it's started, can potentially allow clients to bind to it.
So, a service that was initially started with onStartCommand by a client calling startService can still receive a call to onBind when a client calls bindService. For more information about creating a service that provides binding, see the Bound Services document, which includes more information about the onRebind callback method in the section about Managing the Lifecycle of a Bound Service.
About Android Legal Support. Quicknav Quicknav. Results Loading Content Providers. Android Manifest. User Interface. Input Controls. App Resources. Resource Types. Animation and Graphics. To reiterate this point, Force Stop isn't intended to be graceful and exit the app in a caring manner.
If you have critical code that must be run each time app finishes you need to run it in onPause. When the application gets force stop, Process. Go through this link. You will get some idea. Android force Stop callback to application? It means that the Android system will give this service highest priority and will try not to destroy it when it is running low on system resources. You should only put a Started Service in the foreground if it is critical to do this in order to deliver a compelling user experience.
When a Started Service moves into the foreground, it must display a persistent notification, explicitly notifying the user that the service is running.
This is important because a Started Service in the foreground is detached from the lifecycle of UI components with the exception of the persistent notification. And potentially consuming lots of resources on their phone. STOP integer. Remember that startService Intent can be called multiple times? This is an example of that. This is why the onStartCommand of the Started Service needs to be able to handle Intents that might cause the service to stop itself.
When you want to take your Started Service out of foreground execution, you can call stopForeground true. This will also take away the persistent notification. However, this will not stop the service. In order to do that you still have to call stopSelf. As shown above, pass an Intent with an extra to startService that will be processed by onStartCommand , which will actually call stopSelf on the service.
If there are no clients bound to it, then it will result in onDestroy being called, and the service shutting down. You can also create an explicit Intent pointing to the Service class and pass it to stopService , which will cause stopSelf to be called, and then onDestroy in case there are no bound clients.
Unlike Started Services, Bound Services allow a connection to be established between the Android component binding to the Service, and the Service. This connection is an IBinder which allows methods to be called on the Service.
The simplest example of this is a Bound Service that has a client in a local process. In this case a Java object an Binder subclass is exposed to the client which can be used to access public methods on the Service.
In more complex scenarios where the Bound Service might be executing in a different process from the client, a Message handler or AIDL stub would have to be created. When a client component Activity, Fragment, or another Service connects to a Bound Service, it has an IBinder object which can be used to call methods on the Bound Service. In either case, if the Service Bound or Started needs to send messages to the bound client or whatever component started a Service, it has to use something like LocalBroadcastManager when the the client and Service are local to one process.
In order for a client component Activity, Fragement, another Service to bind to a Bound Service, bindService must be called, with an explicit Intent just like with Started Services. The following skeleton service demonstrates each of the lifecycle methods:. Note: Unlike the activity lifecycle callback methods, you are not required to call the superclass implementation of these callback methods.
Figure 2. The service lifecycle. The diagram on the left shows the lifecycle when the service is created with startService and the diagram on the right shows the lifecycle when the service is created with bindService. Figure 2 illustrates the typical callback methods for a service. Although the figure separates services that are created by startService from those created by bindService , keep in mind that any service, no matter how it's started, can potentially allow clients to bind to it.
A service that was initially started with onStartCommand by a client calling startService can still receive a call to onBind when a client calls bindService. By implementing these methods, you can monitor these two nested loops of the service's lifecycle:. Note : The onCreate and onDestroy methods are called for all services, whether they're created by startService or bindService.
If the service is started, the active lifetime ends at the same time that the entire lifetime ends the service is still active even after onStartCommand returns.
If the service is bound, the active lifetime ends when onUnbind returns. Note: Although a started service is stopped by a call to either stopSelf or stopService , there isn't a respective callback for the service there's no onStop callback.
Unless the service is bound to a client, the system destroys it when the service is stopped— onDestroy is the only callback received. For more information about creating a service that provides binding, see the Bound Services document, which includes more information about the onRebind callback method in the section about Managing the lifecycle of a bound service.
Content and code samples on this page are subject to the licenses described in the Content License. App Basics. Build your first app. App resources. Resource types. App manifest file. Device compatibility. Multiple APK support. Tablets, large screens, and foldables. Build responsive UIs. Build for foldables. Getting started. Handling data. User input. Watch Face Studio.
Health services. Creating watch faces. Android TV. Build TV Apps. Build TV playback apps. Help users find content on TV. Recommend TV content. Watch Next. Build TV games. Build TV input services. TV Accessibility. Android for Cars. Build media apps for cars.
Build navigation, parking, and charging apps for cars. Android Things. Supported hardware. Advanced setup. Build apps. Create a Things app. Communicate with wireless devices. Configure devices. Interact with peripherals. Build user-space drivers. Manage devices. Create a build. Push an update.
Chrome OS devices. App architecture. Architecture Components. UI layer libraries. View binding. Data binding library. Lifecycle-aware components. Paging Library. Paging 2. Data layer libraries. How-To Guides. Advanced Concepts. Threading in WorkManager. App entry points. App shortcuts. App navigation. Navigation component. App links. Dependency injection. Core topics. App compatibility.
Interact with other apps. Package visibility. Intents and intent filters. User interface. Add motion to your layout with MotionLayout. MotionLayout XML reference. Improving layout performance. Custom view components. Look and feel. Splash screens. Add the app bar.
0コメント