Foreground service is getting killed outside without calling selfStop?

Viewed 168

I am working on scheduling app in which i have to run the app until it completes it process but the problem is , Process is getting killed after few minute even it is running on foreground service ...

 class MyService : Service(){

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

    showNotification()
    println("intent ${startId}")
    isServiceStarted =true
    
 val action =   intent?.action

    when(action){
        ADD_SERVICE ->{

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

        DELETE_SERVICE ->{  }
    }



    return START_REDELIVER_INTENT
}

}

 // Manifest file

      <service android:name=".service.MyService "
        android:foregroundServiceType="dataSync"
        />

      <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
1 Answers

you should call startForeground() in your service to attach your service to your application lifecycle to prevent OS from killing your service. foreground-services

Related