I need to start a service at boot time. I searched a lot. They are talking about Broadcastreceiver. As I am new to android development, I didn\'t get a clear picture about s
To Restart service in Android O or more ie OS >28 Use this code KOTLIN VERSION
1) Add permission in manifest
2) Create a Class and extend it with BroadcastReceiver
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import android.util.Log
import androidx.core.content.ContextCompat
class BootCompletedReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, arg1: Intent?) {
Log.d("BootCompletedReceiver", "starting service...")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
ContextCompat.startForegroundService(context, Intent(context, YourServiceClass::class.java))
} else {
context.startService(Intent(context, YourServiceClass::class.java))
}
}
}
3) Declare in Manifest file like this under application tag