I'm just learning about the Services in Android .I have not used any stopService() function but I expect my service to stop working as soon as I kill the app(by closing it and removing it from recent screen) .That's not happening and my Log message continues to print.
This is the service class that I'm using
class NormalServiceClass : Service() {
override fun onBind(p0: Intent?): IBinder? {
return null
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
thread {
while (true) {
Log.d("NORMALSERVICECLASS", "Service working")
Log.d("NORMALSERVICECLASS", Thread.currentThread().name)
Thread.sleep(2000)
}
}
return super.onStartCommand(intent, flags, startId)
}
}
I'm starting the service in my Activity using
startService(Intent(this,NormalServiceClass::class.java))
Why is that happening , Since its not a foreground service .
CodePudding user response:
Do you stop your service?
stopService(Intent(this,NormalServiceClass::class.java));
CodePudding user response:
First of all, you're adding overhead to your system by making an infinite loop like this:
while (true) {
So, solve that error first and secondly to stop the service something like this should be enough.
stopSelfResult(startId)
return super.onStartCommand(intent, flags, startId)