java.lang.InterruptedException
error is thrown onDestroy
because of the Thread.sleep
in the Runnable
. I thought that the Runnable created a new Thread and allowed for sleeps?
MainActivity.kt
class MainActivity : AppCompatActivity() {
lateinit var startBtn: Button
lateinit var stopBtn: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
startBtn = findViewById(R.id.startButton)
stopBtn = findViewById(R.id.stopButton)
startBtn.setOnClickListener { startService() }
stopBtn.setOnClickListener { stopService() }
}
private fun startService() {
val serviceIntent = Intent(this, ForegroundService::class.java)
ContextCompat.startForegroundService(this, serviceIntent)
}
private fun stopService() {
val serviceIntent = Intent(this, ForegroundService::class.java)
stopService(serviceIntent)
}
}
ForegroundService.kt
class ForegroundService : Service() {
private lateinit var thread: Thread
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel("channel_service", "Foreground Service Channel", NotificationManager.IMPORTANCE_DEFAULT)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
val notification: Notification = Notification.Builder(this, "channel_service")
.setContentTitle("Title")
.setContentText("Text")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.build()
startForeground(1, notification)
runnable = TestRunnable()
thread = Thread(runnable)
thread.start()
}
return START_NOT_STICKY
}
override fun onDestroy() {
if(thread.isAlive) {
thread.interrupt()
}
super.onDestroy()
}
override fun onBind(intent: Intent): IBinder? {
return null
}
class TestRunnable : Runnable {
override fun run() {
for(i in 1..15) {
Log.d("Debug", "startThread " + i)
Thread.sleep(1000) //exception through here onDestroy
}
}
}
}