我有问题。用户设置计时器,警报服务在设置的时间发送短信。 如果垃圾收集占用了我的应用程序,则警报服务将发送短信。 但这在奥利奥(Oreo)中不起作用。我该怎么办才能解决问题?我的代码在Oreo之前有效:
清单:
<service android:name="com.kostya500steam.device.service.StartSaunaService"
android:enabled="true"/>
<service android:name="com.kostya500steam.device.service.StopSaunaService"
android:enabled="true"/>
SmsSender:
@SuppressLint("SimpleDateFormat", "NewApi")
private fun initStartAlarm() {
val phoneNo = settings.getString(NUMBER_VALUE, "")
val message = settings.getString(INPUT_CMD_TURN_ON, "")
val intentStart = Intent(context, StartSaunaService::class.java)
val bundle = Bundle()
bundle.putCharSequence(NUMBER_VALUE, phoneNo)
bundle.putCharSequence(INPUT_CMD_TURN_ON, message)
intentStart.putExtras(bundle)
val pendingIntent = PendingIntent.getService(context, 0, intentStart, 0)
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
Log.e("AAA", "before = ${settings.getString(BEFORE_TIMER, " ")}")
alarmManager.setExact(AlarmManager.RTC_WAKEUP,
SimpleDateFormat("yyyy-MM-dd HH:mm")
.parse(settings.getString(BEFORE_TIMER, "")).time, pendingIntent)
}
服务:
override fun onStart(intent: Intent?, startId: Int) {
super.onStart(intent, startId)
intent?.let {
val bundle = intent.extras
val phoneNo = bundle.getCharSequence(NUMBER_VALUE) as String
val message = bundle.getCharSequence(INPUT_CMD_TURN_ON) as String
Log.e("AAA", "Sms включения ушла")
SmsManager.getDefault().sendTextMessage("+$phoneNo", null, message, null, null)
}
}