Doze 模式下 Alram 无法定时唤醒的解决方案

2024-02-09 20:32

本文主要是介绍Doze 模式下 Alram 无法定时唤醒的解决方案,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1. adb 命令模拟进入doze模式

  1. 设置未充电状态

方便连接logcat查看实时日志,正常情况下如果连接 USB 是无法进入doze模式,这个步骤是欺骗系统当前没有连接USB,虽然实际连接得好好的

adb shell dumpsys battery unplug
  1. 设置开启 alarm 日志

并不是每台机器都开启 alarm 的日志,所以我们可以命令强制开启

adb shell dumpsys alarm log on
  1. 强制进入Doze模块

要调试就进入深度休眠模式,即 deep idle mode

adb shell dumpsys deviceidle force-idle deep

2. 复现现象

2.1 Alarm的参考代码
    starAlarmTaskByService(this, 5);public static void starAlarmTaskByService(Context context, int intervalMinute) {AlarmManager mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);long triggerAtMillis = System.currentTimeMillis() + (intervalMinute * 60 * 1000);Log.d(TAG, "starAlarmTaskByService action = " + ACTION_RTC_WAKEUP_ALRTM_TYPE_0_SERVIE  + ", 下次唤醒时刻 = " + DateTimeUtil.getSysDate(triggerAtMillis));Intent intent = new Intent(context, MyService.class);intent.setAction(ACTION_RTC_WAKEUP_ALRTM_TYPE_0_SERVIE);PendingIntent operation = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerAtMillis, operation);} else {mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerAtMillis, operation);}}
2.2 异常日志

注意这里的 flags

2018-08-23 14:25:07.989 934-24435/? V/AlarmManager: set(PendingIntent{c4c0513: PendingIntentRecord{59c2350 test.demo.alarm.zui.com.alarmtest startService}}) : type=0 triggerAtTime=1535005807985 win=0 tElapsed=70204635 maxElapsed=70204635 interval=0 flags=0x1(非DeviceIdleUserWhitelist白名单)
2.3 正常日志

注意这里的 flags

2018-08-23 14:18:29.063 934-945/? V/AlarmManager: set(PendingIntent{c6e22ba: PendingIntentRecord{f55a56b test.demo.alarm.zui.com.alarmtest startService}}) : type=0 triggerAtTime=1535005409060 win=0 tElapsed=69805710 maxElapsed=69805710 interval=0 flags=0x9(DeviceIdleUserWhitelist白名单正常)
2.4 查看对应doze模式白名单

使用命令 adb shell dumpsys deviceidle whitelist 查看

2.4.1 异常现象白名单 1

这个是在系统源码路径 frameworks\base\data\etc\platform.xml,可以手机系统用 adb shell cat “/etc/permissions/platform.xml”查看

D:\AndroidStudioProjects>adb shell dumpsys deviceidle whitelist
system-excidle,com.android.providers.downloads,10020
system-excidle,com.android.shell,2000
system,com.android.providers.downloads,10020
system,com.android.shell,2000
system,test.demo.alarm.zui.com.alarmtest,10090
2.4.2 异常现象白名单 2

这里是没有任何设置白名单

D:\AndroidStudioProjects>adb shell dumpsys deviceidle whitelist
system-excidle,com.android.providers.downloads,10020
system-excidle,com.android.shell,2000
system,com.android.providers.downloads,10020
system,com.android.shell,2000
2.4.3 正常现象的白名单

这里是使用软件接口进行配置 mDeviceIdleService.addPowerSaveWhitelistApp(pkg);

D:\AndroidStudioProjects>adb shell dumpsys deviceidle whitelist
system-excidle,com.android.providers.downloads,10020
system-excidle,com.android.shell,2000
system,com.android.providers.downloads,10020
system,com.android.shell,2000
user,test.demo.alarm.zui.com.alarmtest,10090

3. 相关源码

根据 mDeviceIdleUserWhitelist 进行对应的 flags 的重新设置,这个mDeviceIdleUserWhitelist是使用接口进行加入

    private final IBinder mService = new IAlarmManager.Stub() {@Overridepublic void set(String callingPackage,int type, long triggerAtTime, long windowLength, long interval, int flags,PendingIntent operation, IAlarmListener directReceiver, String listenerTag,WorkSource workSource, AlarmManager.AlarmClockInfo alarmClock) {...省略// If the caller is a core system component or on the user's whitelist, and not calling// to do work on behalf of someone else, then always set ALLOW_WHILE_IDLE_UNRESTRICTED.// This means we will allow these alarms to go off as normal even while idle, with no// timing restrictions.} else if (workSource == null && (callingUid < Process.FIRST_APPLICATION_UID|| callingUid == mSystemUiUid|| Arrays.binarySearch(mDeviceIdleUserWhitelist,UserHandle.getAppId(callingUid)) >= 0)) {flags |= AlarmManager.FLAG_ALLOW_WHILE_IDLE_UNRESTRICTED;flags &= ~AlarmManager.FLAG_ALLOW_WHILE_IDLE;}setImpl(type, triggerAtTime, windowLength, interval, operation, directReceiver,listenerTag, flags, workSource, alarmClock, callingUid, callingPackage);}

4.解决方案

使用软件接口设置doze模式白名单,解决即可,这样就可以开心的准时接收消息了

  • 核心接口 mDeviceIdleService.addPowerSaveWhitelistApp(pkg);
/** Copyright (C) 2015 The Android Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/
package com.android.sufadi.powersave.util;import android.os.IDeviceIdleController;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.ArraySet;
import android.util.Log;/*** Handles getting/changing the whitelist for the exceptions to battery saving features.*/
public class PowerWhitelistBackend {private static final String TAG = "LavaPowerWhitelistBackend";private static final String DEVICE_IDLE_SERVICE = "deviceidle";private static final PowerWhitelistBackend INSTANCE = new PowerWhitelistBackend();private final IDeviceIdleController mDeviceIdleService;private final ArraySet<String> mWhitelistedApps = new ArraySet<>();private final ArraySet<String> mSysWhitelistedApps = new ArraySet<>();public PowerWhitelistBackend() {mDeviceIdleService = IDeviceIdleController.Stub.asInterface(ServiceManager.getService(DEVICE_IDLE_SERVICE));refreshList();}public int getWhitelistSize() {return mWhitelistedApps.size();}public boolean isSysWhitelisted(String pkg) {return mSysWhitelistedApps.contains(pkg);}public boolean isWhitelisted(String pkg) {return mWhitelistedApps.contains(pkg);}public void addApp(String pkg) {try {mDeviceIdleService.addPowerSaveWhitelistApp(pkg);mWhitelistedApps.add(pkg);} catch (RemoteException e) {Log.w(TAG, "Unable to reach IDeviceIdleController", e);}}public void removeApp(String pkg) {try {mDeviceIdleService.removePowerSaveWhitelistApp(pkg);mWhitelistedApps.remove(pkg);} catch (RemoteException e) {Log.w(TAG, "Unable to reach IDeviceIdleController", e);}}private void refreshList() {mSysWhitelistedApps.clear();mWhitelistedApps.clear();try {String[] whitelistedApps = mDeviceIdleService.getFullPowerWhitelist();for (String app : whitelistedApps) {mWhitelistedApps.add(app);}String[] sysWhitelistedApps = mDeviceIdleService.getSystemPowerWhitelist();for (String app : sysWhitelistedApps) {mSysWhitelistedApps.add(app);}} catch (RemoteException e) {Log.w(TAG, "Unable to reach IDeviceIdleController", e);}}public static PowerWhitelistBackend getInstance() {return INSTANCE;}}

这篇关于Doze 模式下 Alram 无法定时唤醒的解决方案的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/695208

相关文章

部署Vue项目到服务器后404错误的原因及解决方案

《部署Vue项目到服务器后404错误的原因及解决方案》文章介绍了Vue项目部署步骤以及404错误的解决方案,部署步骤包括构建项目、上传文件、配置Web服务器、重启Nginx和访问域名,404错误通常是... 目录一、vue项目部署步骤二、404错误原因及解决方案错误场景原因分析解决方案一、Vue项目部署步骤

在MySQL执行UPDATE语句时遇到的错误1175的解决方案

《在MySQL执行UPDATE语句时遇到的错误1175的解决方案》MySQL安全更新模式(SafeUpdateMode)限制了UPDATE和DELETE操作,要求使用WHERE子句时必须基于主键或索引... mysql 中遇到的 Error Code: 1175 是由于启用了 安全更新模式(Safe Upd

Python安装时常见报错以及解决方案

《Python安装时常见报错以及解决方案》:本文主要介绍在安装Python、配置环境变量、使用pip以及运行Python脚本时常见的错误及其解决方案,文中介绍的非常详细,需要的朋友可以参考下... 目录一、安装 python 时常见报错及解决方案(一)安装包下载失败(二)权限不足二、配置环境变量时常见报错及

Java下载文件中文文件名乱码的解决方案(文件名包含很多%)

《Java下载文件中文文件名乱码的解决方案(文件名包含很多%)》Java下载文件时,文件名中文乱码问题通常是由于编码不正确导致的,使用`URLEncoder.encode(filepath,UTF-8... 目录Java下载文件中文文件名乱码问题一般情况下,大家都是这样为了解决这个问题最终解决总结Java下

Idea实现接口的方法上无法添加@Override注解的解决方案

《Idea实现接口的方法上无法添加@Override注解的解决方案》文章介绍了在IDEA中实现接口方法时无法添加@Override注解的问题及其解决方法,主要步骤包括更改项目结构中的Languagel... 目录Idea实现接China编程口的方法上无法添加@javascriptOverride注解错误原因解决方

Java实现状态模式的示例代码

《Java实现状态模式的示例代码》状态模式是一种行为型设计模式,允许对象根据其内部状态改变行为,本文主要介绍了Java实现状态模式的示例代码,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来... 目录一、简介1、定义2、状态模式的结构二、Java实现案例1、电灯开关状态案例2、番茄工作法状态案例

Spring Boot 整合 ShedLock 处理定时任务重复执行的问题小结

《SpringBoot整合ShedLock处理定时任务重复执行的问题小结》ShedLock是解决分布式系统中定时任务重复执行问题的Java库,通过在数据库中加锁,确保只有一个节点在指定时间执行... 目录前言什么是 ShedLock?ShedLock 的工作原理:定时任务重复执行China编程的问题使用 Shed

MYSQL事务死锁问题排查及解决方案

《MYSQL事务死锁问题排查及解决方案》:本文主要介绍Java服务报错日志的情况,并通过一系列排查和优化措施,最终发现并解决了服务假死的问题,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录问题现象推测 1 - 客户端无错误重试配置推测 2 - 客户端超时时间过短推测 3 - mysql 版本问

Android kotlin语言实现删除文件的解决方案

《Androidkotlin语言实现删除文件的解决方案》:本文主要介绍Androidkotlin语言实现删除文件的解决方案,在项目开发过程中,尤其是需要跨平台协作的项目,那么删除用户指定的文件的... 目录一、前言二、适用环境三、模板内容1.权限申请2.Activity中的模板一、前言在项目开发过程中,尤

Linux内存泄露的原因排查和解决方案(内存管理方法)

《Linux内存泄露的原因排查和解决方案(内存管理方法)》文章主要介绍了运维团队在Linux处理LB服务内存暴涨、内存报警问题的过程,从发现问题、排查原因到制定解决方案,并从中学习了Linux内存管理... 目录一、问题二、排查过程三、解决方案四、内存管理方法1)linux内存寻址2)Linux分页机制3)