【问题标题】:AlarmManager running at the wrong timeAlarmManager 在错误的时间运行
【发布时间】:2017-05-31 23:49:22
【问题描述】:

我试图让 AlarmManager 每 3 分钟运行一次,但它在错误和不同的时间运行,以秒为单位的时刻和其他不运行。我正在尝试在 Android 7.0 和另一台 6.0 设备上进行测试,但两者都运行错误,我看到了以下 cmets 但无法修复。

Alarm Manager Example AlarmManager fires alarms at wrong time Android AlarmManager fire at wrong time

以下代码:

long repeatTime = 180000;

        AlarmManager processTimer = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intentAlarm = new Intent(context, TimerProcessReceiver.class);
        PendingIntent pendingIntentAlarm = PendingIntent.getBroadcast(context, 0,
                intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);

        if (android.os.Build.VERSION.SDK_INT < 19) {
            processTimer.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
                    repeatTime, pendingIntentAlarm);
        } else {
            processTimer.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() +
                    repeatTime, pendingIntentAlarm);
        }

还是有问题,我已经更新如上。更新为@Vyacheslav 的回复

long repeatTime = 180000 + System.currentTimeMillis();
    AlarmManager processTimer = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intentAlarm = new Intent(context, ProcessReceiver.class);
    PendingIntent pendingIntentAlarm = PendingIntent.getBroadcast(context, 0,
            intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);

    int currentapiVersion = Build.VERSION.SDK_INT;

    if (Build.VERSION.SDK_INT >= 23) {
        processTimer.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
                repeatTime, pendingIntentAlarm);

    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

            processTimer.setExact(AlarmManager.RTC_WAKEUP,
                    repeatTime, pendingIntentAlarm);

    } else if (currentapiVersion < Build.VERSION_CODES.KITKAT) {

        processTimer.set(AlarmManager.RTC_WAKEUP,  repeatTime,
                pendingIntentAlarm);

    }

如果我同时使用两个计时器,其 PendingIntent 的 ID 为 0 和 1(但添加这些 PendingIntent 的结构与上面的代码相同),但运行时间相同 3 分钟。两者都在几秒钟内随机执行错误的方式。

【问题讨论】:

标签: android timer alarmmanager


【解决方案1】:

请改用setExactAndAllowWhileIdle(int type, long triggerAtMillis, PendingIntent operation)。它有助于唤醒您的设备。

编辑

 int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion < android.os.Build.VERSION_CODES.KITKAT){
            am.set(AlarmManager.RTC_WAKEUP, nexttime, pi);
        } else {
            if (currentapiVersion < android.os.Build.VERSION_CODES.M) {
                am.setExact(AlarmManager.RTC_WAKEUP, nexttime, pi);
            } else {
                    am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, nexttime, pi);
            }
        }

【讨论】:

  • setRepeating 和 setExact,哪些适用于 API 级别 23 以下的设备?如果我在级别 23 测试这些设备,但我也会在较低级别进行测试。抱歉弄错了。
  • @user6505882 我正在使用这个:查看更新的答案。
  • @user6505882 由于操作系统的限制,这是不可能改善 presition 的。在大多数情况下,大约 1-2 秒是正常的。在新的操作系统版本中存在节电模式
  • @user6505882 这是你可以达到的最大值
  • 到目前为止还没有解决方案,有没有人设法让这个 AlarmManager 与两个 PendingIntents 一起工作,计时器在正确的时间独立运行?其中一个计时器将在应用程序中的某个时间点终止,但另一个将继续,但在 3 分钟内都没有工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-23
  • 1970-01-01
  • 1970-01-01
  • 2018-09-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多