我一直在研究如何安排针对 Oreo + 设备的警报。我目前的方法是使用AlarmManager
铃声服务和广播接收器。我让警报响起并显示通知,但 Oreo + 设备不会触发通知和警报。
我了解到,JobIntentService
由于Doze
和其他背景限制,使用是一种很好的做法。目前,当使用 a 时JobIntentService
,我收到一个错误:
java.lang.RuntimeException:执行 doInBackground() 时出错
我做错了吗?我真的需要实施AsyncTask
吗?
class AlarmReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val alarmStatus: String = intent!!.getStringExtra("alarmStatus")
val serviceIntent = Intent(context, RingtoneService::class.java)
serviceIntent.putExtra("alarmStatus", alarmStatus)
//context!!.startService(serviceIntent)
RingtoneService.enqueueWork(context, serviceIntent)
}
}
class RingtoneService : JobIntentService() {
// Enqueing work to do in this service.
companion object {
val SERVICE_JOB_ID = 1
fun enqueueWork(context: Context?, work: Intent) {
if (context != null) {
enqueueWork(context, RingtoneService::class.java, RingtoneService.SERVICE_JOB_ID, work)
}
}
}
override fun onHandleWork(intent: Intent) {
onHandleIntent(intent)
}
private fun onHandleIntent(intent: Intent) {
//handling of notification goes here
}
}