setExact is more reliable than setRepeating on some ROMs (e.g., MIUI).

This commit is contained in:
azuo
2021-09-13 23:24:58 +08:00
parent 818b4ec0ba
commit 9f47d626a9
6 changed files with 26 additions and 21 deletions

View File

@ -22,13 +22,8 @@ class WeatherReceiver : BroadcastReceiver() {
Intent.ACTION_MY_PACKAGE_REPLACED,
Intent.ACTION_TIMEZONE_CHANGED,
Intent.ACTION_LOCALE_CHANGED,
Intent.ACTION_TIME_CHANGED -> setUpdates(context)
Actions.ACTION_WEATHER_UPDATE -> {
GlobalScope.launch(Dispatchers.IO) {
WeatherHelper.updateWeather(context)
}
}
Intent.ACTION_TIME_CHANGED,
Actions.ACTION_WEATHER_UPDATE -> setUpdates(context)
}
}
@ -46,13 +41,15 @@ class WeatherReceiver : BroadcastReceiver() {
else -> 60
}
with(context.getSystemService(Context.ALARM_SERVICE) as AlarmManager) {
setRepeating(
setExact(
AlarmManager.RTC,
Calendar.getInstance().timeInMillis,
interval,
System.currentTimeMillis() + interval,
PendingIntent.getBroadcast(context, 0, Intent(context, WeatherReceiver::class.java).apply { action = Actions.ACTION_WEATHER_UPDATE }, 0)
)
}
GlobalScope.launch(Dispatchers.IO) {
WeatherHelper.updateWeather(context)
}
}
}