【问题标题】:Sometimes alarms doesn't get triggered using AlarmManager有时使用 AlarmManager 不会触发警报
【发布时间】:2018-01-04 14:44:43
【问题描述】:

我有一个提醒应用程序,它会通知用户特定任务。一切都按预期工作,但有时没有触发警报。近 100 位用户报告了此问题。我怀疑当设备处于睡眠模式时,不会触发警报。我面临这个问题将近 1 年了,我还没有找到任何解决方案。

这是安排警报的方式:

public void schedule(Context context, long nextTrigger, Intent intent) {
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, REQ_CODE_ALARM, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    int ALARM_TYPE = AlarmManager.RTC_WAKEUP;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        alarmManager.setExactAndAllowWhileIdle(ALARM_TYPE, nextTrigger, pendingIntent);
    }
    else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        alarmManager.setExact(ALARM_TYPE, nextTrigger, pendingIntent);
    }
    else {
        alarmManager.set(ALARM_TYPE, nextTrigger, pendingIntent);
    }
}

这是来自未决意图的广播接收器:

public class ReminderAlarm extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = null;
        if (pm != null) {
            wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, APP_NAME);
            if (wl != null)
                wl.acquire(10000L);
        }

        Log.log("Reminder alarm received");

        ReminderJobIntentService.enqueueWork(context, ReminderJobIntentService.class, JOB_ID_REMINDER_ALARM, intent);

        if (wl != null)
            wl.release();
    }
}

这是从广播中排队的 Job Intent 服务:

public class ReminderJobIntentService extends JobIntentService {

    @Override
    protected void onHandleWork(@NonNull Intent intent) {
        Log.log("Reminder job intent service");

        // Creates a notification on the UI
        Utils.createNotification();

        // Schedule the next task (alarm). Actually it's schedule(context, long, intent) but it calculates the next trigger. That's it
        Scheduler.getInstance().scheduleNext();
    }
}
  1. 我有日志显示警报安排正确。收到广播时(触发警报时)我也在记录这个。当广播没有日志时,表示没有触发警报。
  2. 警报安排在Boot Complete
  3. 我已尝试使用不同的 Android 设备和版本多次重现此问题,但无法重现。我什至尝试将设备放入Doze ModeBattery Unpluged,但没有运气。警报被触发。我将近 3 天没有动过设备,警报延迟 4 分钟触发(因为 Android 操作系统正在批处理警报)。
  4. 当用户抱怨没有收到提醒时,通常我会将他们重定向到“设置 -> 电池 -> 忽略应用程序的电池优化”。但有时这并不能解决问题。

我注意到抱怨此问题的人使用的是三星设备:搭载 Android 6、7 的 S6、S7、S8 型号。(可能是因为我 45% 的用户使用这种类型的设备?)

你有没有遇到过这个特定的问题?我错过了什么吗?三星机型是否会在后台杀死应用程序?也许某些 3rd 方应用正在扼杀这些应用?

【问题讨论】:

  • 很想知道您是否找到了解决此问题的方法

标签: android alarmmanager android-alarms


【解决方案1】:

我在构建应用程序以获取每日通知时遇到了类似的问题。问题不在于代码,而在于 Android Kitkat 及更高版本中处理警报的方式。警报现在进行了优化,以最大限度地减少唤醒和电池使用。所以他们可能会在不同的时间开火或根本不开火。您可以在这里查看更多信息:

AlarmManager not working properly

【讨论】:

  • 那么解决办法是什么?有没有什么健壮的方法行得通?!这对于 Android 来说非常尴尬,因为它没有一个万无一失的、可靠的重要警报方式....
  • @doctorram 已经有一段时间了,但据我所知,我为特定的 API 版本调用了特定的 alarmManager 函数。大部分时间都在工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-23
相关资源
最近更新 更多