Android4.4及之后休眠状态下Alarm不准时的问题

为了减轻功耗,延长电池使用时间。Android 4.4及之后的版本号採用非精准闹钟机制。以及休眠状态下的wakeup类型的alarm不会实时唤醒设备,而会等到机器被物理唤醒时才触发alarmAndroid 6.0提供了新的api:setExactAndAllowWhileIdle()部分解决问题,但依旧不能在休眠状态下精准唤醒。

关于alarm api 的支持与使用请參考下图:



(图片来源:https://plus.google.com/+AndroidDevelopers/posts/GdNrQciPwqo)

此外,应用层面不要使用不持有wakelockBroadcastReceiver,而要使用WakefulBroadcastReceiver

为了修复这个问题。以5.0.2版本号为例,须要改动内核下的alarm-dev.c以及framework下的AlarmManagerService。

  • 内核 alarm-dev.c:其原因是使用普通的static struct wakeup_source alarm_wake_lock;,而非带有WAKE_LOCK_SUSPEND类别信息的struct wake_lock,而且须要使用带有android_前缀的wakeup lock相关函数。

    即:

    android_wake_lock_init
    android_wake_lock_destroy
    android_wake_lock
    android_wake_lock_timeout
    android_wake_unlock

    而非普通的wake lock操作函数:

    wake_lock_init
    wake_lock_destroy
    wake_lock
    wake_lock_timeout
    wake_unlock
  • framework AlarmManagerService.java:须要将wakeup类型的alarm特殊处理:即精准闹铃。在setImpl中加入例如以下代码:

    public boolean isWakeup(int type)
    {
    return (type & TYPE_NONWAKEUP_MASK) == 0;
    } void setImpl(int type, long triggerAtTime, long windowLength, long interval,
    PendingIntent operation, boolean isStandalone, WorkSource workSource,
    AlarmManager.AlarmClockInfo alarmClock) {
    if (operation == null) {
    Slog.w(TAG, "set/setRepeating ignored because there is no intent");
    return;
    } if (isWakeup(type)) {
    windowLength = AlarmManager.WINDOW_EXACT;
    isStandalone = true;
    }

    并在alarm被触发时多取几个满足条件的batch做处理:

    boolean triggerAlarmsLocked(ArrayList<Alarm> triggerList, final long nowELAPSED,
    final long nowRTC) {
    boolean hasWakeup = false;
    // batches are temporally sorted, so we need only pull from the
    // start of the list until we either empty it or hit a batch
    // that is not yet deliverable ArrayList<Alarm> repeatList = new ArrayList<Alarm>();
    ListIterator<Batch> it = mAlarmBatches.listIterator();
    while (it.hasNext()) {
    Batch batch = it.next();
    if (batch.start > nowELAPSED) {
    // Everything else is scheduled for the future
    break;
    } // We will (re)schedule some alarms now; don't let that interfere
    // with delivery of this current batch
    it.remove(); final int N = batch.size();
    for (int i = 0; i < N; i++) {
    Alarm alarm = batch.get(i);
    alarm.count = 1;
    triggerList.add(alarm); // Recurring alarms may have passed several alarm intervals while the
    // phone was asleep or off, so pass a trigger count when sending them.
    if (alarm.repeatInterval > 0) {
    // this adjustment will be zero if we're late by
    // less than one full repeat interval
    alarm.count += (nowELAPSED - alarm.whenElapsed) / alarm.repeatInterval; // Also schedule its next recurrence
    repeatList.add(alarm);
    } if (alarm.wakeup) {
    hasWakeup = true;
    mNextWakeup = 0;
    } // We removed an alarm clock. Let the caller recompute the next alarm clock.
    if (alarm.alarmClock != null) {
    mNextAlarmClockMayChange = true;
    }
    }
    } if (repeatList.size() > 0) {
    for (Alarm alarm : repeatList) {
    final long delta = alarm.count * alarm.repeatInterval;
    final long nextElapsed = alarm.whenElapsed + delta;
    setImplLocked(alarm.type, alarm.when + delta, nextElapsed, alarm.windowLength,
    maxTriggerTime(nowELAPSED, nextElapsed, alarm.repeatInterval),
    alarm.repeatInterval, alarm.operation, alarm.windowLength == AlarmManager.WINDOW_EXACT, true,
    alarm.workSource, alarm.alarmClock, alarm.userId);
    }
    } // This is a new alarm delivery set; bump the sequence number to indicate that
    // all apps' alarm delivery classes should be recalculated.
    mCurrentSeq++;
    calculateDeliveryPriorities(triggerList);
    Collections.sort(triggerList, mAlarmDispatchComparator); return hasWakeup;
    }
  • 应用层面使用WakefulBroadcastReceiver:

    import android.support.v4.content.WakefulBroadcastReceiver;
    
    public class AutoUpdateAlarmReceiver extends WakefulBroadcastReceiver {
    
        @Override
    public void onReceive(Context context, Intent intent) {
    // Start the service, keeping the device awake while the service is
    // launching. This is the Intent to deliver to the service.
    Intent service = new Intent(context, AutoUpdateIntentService.class);
    service.setAction(intent.getAction());
    startWakefulService(context, service);
    }
    }

    对应的IntentService例如以下所看到的:

    public class AutoUpdateIntentService extends IntentService {
    public AutoUpdateIntentService() {
    super("AutoUpdateIntentService");
    } @Override
    protected void onHandleIntent(Intent intent) {
    String action = intent.getAction(); // do your work here.
    // ... // Release the wake lock provided by the WakefulBroadcastReceiver.
    AutoUpdateAlarmReceiver.completeWakefulIntent(intent);
    }
    }

最新文章

  1. 评价cnblogs.com的用户体验
  2. 多线程编程-工具篇-BlockingQueue
  3. 深入理解javascript原型和闭包(完结)
  4. Mesos源码分析
  5. codeforces Round #263(div2) D. Appleman and Tree 树形dp
  6. linux中如何查看进程占用了哪些端口?
  7. iOS开发网络篇—大文件的多线程断点下载(转)
  8. 转 android学习—— context 和 getApplicationContext()
  9. position:sticky 定位 position:fixed
  10. Demo4
  11. golang基础- ElasticSearch搜索引擎、kibana可视化工具、向ES输出数据
  12. Web 前端编程运维必备
  13. python采集百度搜索结果带有特定URL的链接
  14. 【读书笔记】iOS-设计模式
  15. Luogu P2403 [SDOI2010]所驼门王的宝藏
  16. SQL 经典回顾:JOIN 表连接操作不完全指南
  17. Java基础知识_毕向东_Java基础视频教程笔记(14-18集合框架)
  18. FineBI表单如何更新
  19. VC++ 6.0开发套件(自己收藏!)
  20. json中把非json格式的字符串转换成json对象再转换成json字符串

热门文章

  1. java中遍历实体类属性和类型
  2. 解决Windows server 2012 R2 系统使用IIS8浏览Asp程序出现&quot;An error occurred on the server when processing the URL&quot;错误
  3. Microsoft Bot Builder Overview
  4. c++初始化函数列表
  5. Nginx.conf简介
  6. js 获取iframe页面元素
  7. javascript 新知识
  8. ubuntu16.4安装后做的事情
  9. DBCP( 二) DataBase Connection Pool 的使用
  10. Android高效异步图片加载框架