Services In Kotlin

Introduction

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

<? xml version="1.0" encoding = "utf-8"?>      
<mnifest ..>      
  <application ..>      
         <activity ..>      
         </activity ..>      
         <service      
              android:name = ".MyService"      
              android:enabled = "true"      
              android:exported = "true" >      
         </service>      
   </application>      
</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.

val intent = = Intent(this, TheService::class.java)  
startService(intent)  
//normal service      
val intent = Intent(this, TheService::class.java)  
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {  
    StartForegroundService(intent)  
} else {  
    startService(intent)  
}  

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.

<service    
         android: name= ".MyService"    
         android: enabled= "true"    
         android: exported= "true">    
 <intent - filter>    
        <action android:name="<PCKG_NAME>.START_SERVICE" />    
</intent - filter>    
</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.

class MyBinder(val servc: MyService): Binder() {  
    fun getService(): MyService {  
        return servc  
    }  
}  
class MyService: Service() {  
    private val binder: IBinder = MyBinder(this)  
    private val generator: RandomRandom = Random()  
    override  
    fun onBind(intent: Intent): IBinder {  
        return binder  
    }  
    fun getRandomNumber(): Int {  
        return generator.NextInt(100)  
    }  
}   

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()
我来评几句
登录后评论

已发表评论数()

相关站点

+订阅
热门文章