Services In Kotlin


Services are components that run without a user interface and have a conceptual affinity toward long-running processes. They are separate from notifications in the status bar or a Toast. Services can be started by application or bound by the application.
Services have two flavors: foreground services and background services.

Foreground Services

The intrinsic functioning of the foreground services differs with different versions. A foreground service follows a special notation and receive improved attention from the OS, making them less like to be killed because of a resource shortage.
Before Android 8.0, there were services that just presented a notification entry in the status bar. The client component that needs to use a service doesn't know whether the service that started is a foreground service. It just starts service with startService.
Then they run with the user being made aware of them. They must interfere with the operating system via notification in the status bar. A client component explicitly starts a foreground service itself.
Characteristics of foreground service are that it is less likely to be killed by Android because of the available shortage.

Background Services

Background services run in the background and will not show an entry in the status bar. They allowed using toasts to send short notification messages to users. Background services are more brittle compared to foreground services to user activities to kill them when there is a resource shortage.
The app has a visible activity currently active or paused.
The app has a foreground service, or the service is startForeground() at its operation.
Another foreground app is connected by using one of its services or by the content provider.
Declaring services
  1. <? xml version="1.0" encoding = "utf-8"?>      
  2. <mnifest ..>      
  3.   <application ..>      
  4.          <activity ..>      
  5.          </activity ..>      
  6.          <service      
  7.               android:name = ".MyService"      
  8.               android:enabled = "true"      
  9.               android:exported = "true" >      
  10.          </service>      
  11.    </application>      
  12. </maifest>     
Name is a service class and it uses a dot at first character and prepends it with the name of the package specified in the manifest element. Enabled is either true or false and its default value is true or else the service is effectively disabled. Exported specifies other application can use the service and the default value is set to false.

Starting Service

A service is explicitly started from any component and it is a subclass of android.content.Context or has access to Context. This is the case for activities other services broadcast receivers and content providers.
  1. val intent = = Intent(this,  
  2. startService(intent)  
  3. //normal service      
  4. val intent = Intent(this,  
  5. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {  
  6.     StartForegroundService(intent)  
  7. else {  
  8.     startService(intent)  
  9. }  
An external service is a service that is needs to be started in part of another application and needs an intent filter inside the sevice declaration.
  1. <service    
  2.          android: name= ".MyService"    
  3.          android: enabled= "true"    
  4.          android: exported= "true">    
  5.  <intent - filter>    
  6.         <action android:name="<PCKG_NAME>.START_SERVICE" />    
  7. </intent - filter>    
  8. </service>     
<PCKG_NAME> is the name of application package and diffrent identifier can be used by avoiding START_SERVICE.The start and stop external service is inside the intent constructor. 

Binding to service

Running a service is the next process to start the service and it is referred to as binding the service. To create a service it must bind to the same Appication. This is used to bind the service.
  1. class MyBinder(val servc: MyService): Binder() {  
  2.     fun getService(): MyService {  
  3.         return servc  
  4.     }  
  5. }  
  6. class MyService: Service() {  
  7.     private val binder: IBinder = MyBinder(this)  
  8.     private val generator: RandomRandom = Random()  
  9.     override  
  10.     fun onBind(intent: Intent): IBinder {  
  11.         return binder  
  12.     }  
  13.     fun getRandomNumber(): Int {  
  14.         return generator.NextInt(100)  
  15.     }  
  16. }   
Message sent from the service client to the service is sent by putting data in the opposite direction, from the service to the service client. It can be achieved by using extraMessanger inside client.

Service lifecycle

To implement a service, it need to overwrite the lifecycle callbacks. Serices are more readily subject to stop forced by os, they may require attention in service clients.
  • oncreate()
  • onStartCommand()
  • onBind()
  • onUnbind()
  • onRebind() 
  • onDestroy()

Similar Articles