ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

三星Android警报setExactAndAllowWhileIdle意外行为

2019-06-08 23:14:21  阅读:391  来源: 互联网

标签:android service android-alarms


我的Android应用程序正在使用Marshmallow上的setExactAndAllowWhileIdle运行重复警报.警报每10秒发生一次,并通过使用权限REQUEST_IGNORE_BATTERY_OPTIMIZATIONS避免打盹模式(设备同意忽略此应用程序的电池优化)

 public void startAlarm(Intent alarmIntent, int delayMs, int alarmId) {
    PendingIntent recurringAlarm = PendingIntent.getBroadcast(context.getApplicationContext(), alarmId,
            alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Calendar updateTime = Calendar.getInstance();
    alarms.cancel(recurringAlarm);
    if (Build.VERSION.SDK_INT >= 23) {
        alarms.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis() + delayMs, recurringAlarm);
    } else if (Build.VERSION.SDK_INT >= 19) {
        alarms.setExact(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis() + delayMs, recurringAlarm);
    } else {
        alarms.set(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis() + delayMs, recurringAlarm);
    }
}

此警报启动服务.然后,该服务使用相同的方法(上面)创建10秒的另一个警报.我认为此警报将每10秒发生一次,因为忽略了打盹模式,但在极少数情况下,警报将被推迟更长时间:

> 53分6秒
> 18分40秒
> 54分钟37分19秒
> 1天48分30秒

什么可能导致这种奇怪的行为?这似乎只发生在三星设备上.

解决方法:

官方文件说明

To reduce abuse, there are restrictions on how frequently these alarms will go off for a particular application. Under normal system operation, it will not dispatch these alarms more than about every minute (at which point every such pending alarm is dispatched); when in low-power idle modes this duration may be significantly longer, such as 15 minutes.

我怀疑三星已做出修改,使这段时间超过15分钟.他们以进行深度修改而闻名,这些修改通常会导致意外行为.

标签:android,service,android-alarms
来源: https://codeday.me/bug/20190608/1201024.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有