首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >AlarmManager:未设置/触发新警报

AlarmManager:未设置/触发新警报
EN

Stack Overflow用户
提问于 2021-10-01 03:39:22
回答 1查看 24关注 0票数 1

我有一个应用程序,其中有一个选项,设置在每天的特定时间提醒。问题是,对于一些用户来说,他们永远不会得到提醒。我有一些日志记录,然后应用程序启动,对他们来说,警报没有设置,即使它应该设置。

只有一些用户有这个问题,我不能自己重现它,我也看不到错误是什么!

我这样设置提醒:

代码语言:javascript
运行
AI代码解释
复制
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)
}

获取提醒时间:

代码语言:javascript
运行
AI代码解释
复制
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)
}

接收器看起来像

代码语言:javascript
运行
AI代码解释
复制
class AlarmReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        scheduleReminder(context = context, true)
        showReadReminderNotification(context = context)
    }
}

我还有一个开机接收器:

代码语言:javascript
运行
AI代码解释
复制
class BootReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        if (intent.action == "android.intent.action.BOOT_COMPLETED") {
            scheduleReminder(context = context)
        }
    }
}

和清单:

代码语言:javascript
运行
AI代码解释
复制
<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的其他几个帖子进行了比较,但我看不出问题是什么。

每次应用程序启动时,我都会像这样检查提醒,如果设置了闹钟,如果共享偏好设置了应该设置提醒的信息,但对于许多用户来说,这个日志会被触发,即使应该设置闹钟也没有设置。

代码语言:javascript
运行
AI代码解释
复制
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
}

谁能发现错误是什么,因为我肯定不能。:(

EN

回答 1

Stack Overflow用户

发布于 2021-10-01 03:44:17

也许是权限问题?

代码语言:javascript
运行
AI代码解释
复制
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69405281

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档