Añadir en AndroidManifest.xml
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
Y la declaración del servicio
<service
android:name=".MyService"
android:enabled="true"
android:exported="false" />
Añadir los iconos correspondientes play y stop
Añadir la extensión de funcionalidad kotlin
fun Context?.toast(text: CharSequence, duration: Int = Toast.LENGTH_LONG) = this?.let { Toast.makeText(it, text, duration).show() }
fun Context?.toast(@StringRes textId: Int, duration: Int = Toast.LENGTH_LONG) = this?.let { Toast.makeText(it, textId, duration).show() }
@Suppress("DEPRECATION")
fun <T> Context.isServiceRunning(service: Class<T>): Boolean {
return (getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager)
.getRunningServices(Integer.MAX_VALUE)
.any { it.service.className == service.name }
}