我有一个应用程序,其中有一个选项,设置在每天的特定时间提醒。问题是,对于一些用户来说,他们永远不会得到提醒。我有一些日志记录,然后应用程序启动,对他们来说,警报没有设置,即使它应该设置。
只有一些用户有这个问题,我不能自己重现它,我也看不到错误是什么!
我这样设置提醒:
fun scheduleReminder(context: Context, reschedule: Boolean = false) {
val alarmManager = context.getSystemService(ALARM_SERVICE) as AlarmManager
val alarmPendingIntent by lazy {
val intent = Intent(context, AlarmReceiver::class.java)
PendingIntent.getBroadcast(context, 0, intent, 0)
}
// Get the time from the settings, LocalTime
val time = getReminderTime(context = context)
val diff = kotlin.math.abs(ChronoUnit.MINUTES.between(time, LocalTime.now()).toInt())
// If the time is before current time, not far in the future or if it's from previous
// triggered alarm, schedule next alarm for tomorrow
val shouldSetForTomorrow = diff < 30 || time < LocalTime.now() || reschedule
val calendar = GregorianCalendar.getInstance().apply {
if (shouldSetForTomorrow)
add(Calendar.DAY_OF_MONTH, 1)
set(Calendar.HOUR_OF_DAY, time.hour)
set(Calendar.MINUTE, time.minute)
set(Calendar.SECOND, 0)
set(Calendar.MILLISECOND, 0)
}
if (Build.VERSION.SDK_INT >= 31)
alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, alarmPendingIntent)
if (Build.VERSION.SDK_INT >= 23)
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, alarmPendingIntent)
else
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, alarmPendingIntent)
}
获取提醒时间:
fun getReminderTime(context: Context): LocalTime {
val sharedPref = context.getSharedPreferences("reminder", Context.MODE_PRIVATE)
val secondsOfDay = sharedPref.getInt(REMINDER_TIME, 0)
val hours = secondsOfDay / 3600
val minutes = (secondsOfDay - 3600 * hours) / 60
return LocalTime.of(hours, minutes)
}
接收器看起来像
class AlarmReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
scheduleReminder(context = context, true)
showReadReminderNotification(context = context)
}
}
我还有一个开机接收器:
class BootReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == "android.intent.action.BOOT_COMPLETED") {
scheduleReminder(context = context)
}
}
}
和清单:
<receiver android:name="com.lostpixels.biblept.notifications.AlarmReceiver" android:enabled="true"/>
<receiver android:name="com.lostpixels.biblept.notifications.BootReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
我已经与Stackoverflow的其他几个帖子进行了比较,但我看不出问题是什么。
每次应用程序启动时,我都会像这样检查提醒,如果设置了闹钟,如果共享偏好设置了应该设置提醒的信息,但对于许多用户来说,这个日志会被触发,即使应该设置闹钟也没有设置。
if (!isReminderSet(context = this) && userWantsReminder(context = this)) {
Log("MainActivity", "Reminder not set when it should be")
scheduleReminder(context = this)
}
fun userWantsReminder(context: Context): Boolean {
return context.getSharedPreferences("reminder", Context.MODE_PRIVATE)
.getBoolean(REMINDER_SET, false)
}
fun isReminderSet(context: Context): Boolean {
return PendingIntent.getBroadcast(
context, 0,
Intent(context, AlarmReceiver::class.java),
PendingIntent.FLAG_NO_CREATE
) != null
}
谁能发现错误是什么,因为我肯定不能。:(
发布于 2021-10-01 03:44:17
也许是权限问题?
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
https://stackoverflow.com/questions/69405281
复制相似问题