'Can we still launch a foreground service with extended Service class or we should move to workmanager?

I am getting issue regarding foreground service is that some time foreground being killed by os so should i resolve Service class issue or move to workmanager . i have completed the application and only getting a single os issue suggestion required thanks . // this is the service class

          class SmsService  : Service() {
   private var wakeLock: PowerManager.WakeLock? = null
private lateinit var smsScheduler : SmsScdeduler
private var isServiceStarted  = false
private   var    notificationBuilder : NotificationCompat.Builder?=null
private var serviceScope = CoroutineScope(Dispatchers.Main)
private var  notificationManager : NotificationManager?=null


fun isServiceRunning():Boolean{
    return isServiceStarted }

companion object{
    const  val MESSAGE_ID: String ="messageId"
    const val USER_ID:String ="userId"
    const val DELIVERED: String ="sb.app.messageschedular.sms_schedulers.DELIVERYKEY"
    const val ADD_SERVICE = "sb.spp.message_scheduler.add"
    const val DELETE_SERVICE ="sb.spp.message_scheduler.delete"
    const val Add_kEY ="ADD_KEY"
    const val SENT ="sb.app.messageschedular.sms_schedulers.SENT"
    private  const val channelId = "default_notification_channel_id"
    private const val NOTIFICATION_ID =1995L
}

/************* Delivery BroaddCast ENd  **************/



override fun onCreate() {
    super.onCreate()


    smsScheduler =  SmsScdeduler.getInstance(this )




    val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
    notificationBuilder  = NotificationCompat.Builder(this, channelId)
        .setSmallIcon(sb.app.messageschedular.R.mipmap.ic_launcher)
        .setContentTitle("Message Scheduled")
        //  .setContentIntent(pendingIntent)
        .setAutoCancel(true)

}

@RequiresApi(Build.VERSION_CODES.O)
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {

    showNotification(NOTIFICATION_ID)

    wakeLock =
        (getSystemService(Context.POWER_SERVICE) as PowerManager).run {
            newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "EndlessService::lock").apply {
                acquire()
            }
        }

    println("intent ${intent }")
    isServiceStarted =true

    if(intent!=null) {
        val action = intent?.action
        val sms = intent?.getParcelableExtra<Sms>(Add_kEY)
        schedule(sms!!)
    }else{

        serviceScope.launch(Dispatchers.IO) {
            smsScheduler.reSchedule()
        }


    }

    return START_STICKY }

@RequiresApi(Build.VERSION_CODES.O)
private fun showNotification(messageid:Long ) {

    notificationManager  = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val channel = NotificationChannel(channelId, "test1", NotificationManager.IMPORTANCE_DEFAULT)
        channel.description = "test otificaiton"
        notificationManager?.createNotificationChannel(channel)
    }

    this.startForeground(messageid.toInt() ,notificationBuilder!!.build())


}

override fun onBind(intent: Intent?): IBinder? {
    return LocalBinder() }



inner class LocalBinder : Binder(){
    fun getService(): SmsService = this@SmsService
}



override fun onDestroy() {
    super.onDestroy()
    isServiceStarted =false
    if( serviceScope.isActive){
        println("Service Scope is cancelled ")
        serviceScope.cancel() }

 
}




}

fun finish() {
    println("service finished ")

    println("Stop  self")

    println("Stop foreground service")
    stopForeground(true )

    println("service state  false ")
    isServiceStarted =false
    stopSelf()
}




}


// calling service 
                fun scheduleService(sms: Sms) {
    if(!mService.isServiceRunning()){

        val intent = Intent(this , SmsService::class.java)
        intent.action = SmsService.ADD_SERVICE
        intent.putExtra(SmsService.Add_kEY, sms)

      
   ContextCompat.startForegroundService(this.applicationContext,intent)

      }
      

/// Permission of Service

    <service android:name=".service.SmsService"

        android:foregroundServiceType="dataSync"
        />


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source