在S5 Neo上的睡眠模式下,AlarmManager无法按预期工作 [英] AlarmManager not working as expected in sleep mode on S5 Neo

查看:117
本文介绍了在S5 Neo上的睡眠模式下,AlarmManager无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在服务中使用AlarmManager,每分钟都会触发一次。

I am using an AlarmManager in a Service to be triggered every minute.

    PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0,
            getUpdateServiceIntent(mContext), PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);

    // Cancel any pending Intent
    am.cancel(pendingIntent);

    // Set a new one
    am.set(AlarmManager.RTC_WAKEUP, 60000, pendingIntent);

在Samsung S5 Neo上:
屏幕处于活动状态时,它可以正常工作。
当屏幕关闭时,它每5分钟触发一次(而不是一个)。

On the Samsung S5 Neo : When the screen is active, it is working as expected. When the screen is off, it is triggered every 5 minutes (instead of one).

我在S5 Mini(使用Android 4.4)上尝试了完全相同的代码),Nexus 5 5.1和Nexus 5 6.0,此代码正常运行。

I try this exact same code on S5 Mini (with Android 4.4), Nexus 5 5.1 and Nexus 5 6.0, this code is working fine.

targetSdkVersion为19。

targetSdkVersion is 19.

任何想法如何在屏幕关闭时保持AlarmManager正常工作?
即使我要求30秒,延迟仍然是5分钟。

Any idea how to keep the AlarmManager working correctly when screen is off ? The delay is still 5 minutes, even if I ask for 30 seconds.

编辑:
我也尝试了'setExact'方法,但是它没有改变任何东西。每次警报之间还有5分钟的间隔。

EDIT : I also tried the 'setExact' method, but it didn't change anything. Still have a 5 minutes interval between each alarm.

推荐答案

您可能应该使用

AlarmManager#setExact(int type,long triggerAtMillis,PendingIntent操作)

而不是

AlarmManager#set(int type,long triggerAtMillis,PendingIntent操作)

看看

来自Google:

AlarmManager#set(int type,long triggerAtMillis ,PendingIntent操作))


注意:从API 19开始,将处理传递给此方法的触发时间不精确:警报不会在此时间之前发送,但可能会延迟并在一段时间后发送。操作系统将使用此策略,以便将警报在整个系统中分批在一起,从而最大程度地减少设备需要唤醒的次数,并最大程度地减少电池消耗。通常,只要将来的警报已调度,就不会延迟不久的警报。

Note: Beginning in API 19, the trigger time passed to this method is treated as inexact: the alarm will not be delivered before this time, but may be deferred and delivered some time later. The OS will use this policy in order to "batch" alarms together across the entire system, minimizing the number of times the device needs to "wake up" and minimizing battery use. In general, alarms scheduled in the near future will not be deferred as long as alarms scheduled far in the future.

编辑:

我在报警应用程序中使用什么:

What i am using for an alarm application :

清单:

<使用权限android:name = android.permission.WAKE_LOCK />

Java代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(nextAlarm.getTimeInMillis(), pendingIntent);
  alarmManager.setAlarmClock(alarmClockInfo, pendingIntent);
}else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  alarmManager.setExact(android.app.AlarmManager.RTC_WAKEUP, nextAlarm.getTimeInMillis(), pendingIntent);
}else {
  alarmManager.set(android.app.AlarmManager.RTC_WAKEUP, nextAlarm.getTimeInMillis(), pendingIntent);
}

这篇关于在S5 Neo上的睡眠模式下,AlarmManager无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆