【发布时间】:2020-02-02 05:25:54
【问题描述】:
当操作系统进入打盹模式时,我正在尝试测试我的 Android 应用的行为。我正在使用运行 Android API 25 的 gennymotion 模拟器。应用程序使用类型为 RTC_WAKEUP 的方法 setExact 通过 AlarmManager 启动 IntentService。我将警报设置为在 1 分钟后触发(仅用于测试目的)。
这是意图服务代码(MyService.java):-
public class MyService extends IntentService {
public static final String TAG = "noor";
@Override
public void onHandleIntent(Intent intent) {
for (int i = 1; i <= 10; i++) {
Log.d(TAG, "i: " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
这是警报管理器代码:-
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, MyService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 101, intent, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarm.setExact(AlarmManager.RTC_WAKEUP,System.currentTimeMillis() + (60 * 1000),pendingIntent);
}
}
只是为了确保我成功通过运行以下建议的 dumpsys 命令将模拟器置于 IDLE 状态:-
adb shell dumpsys deviceidle enable
adb shell dumpsys battery unplug
adb shell dumpsys deviceidle force-idle
我什至用
仔细检查过adb shell dumpsys deviceidle get deep
现在问题来了:-
即使设备处于空闲状态,我仍然能够看到警报正在启动的 IntentService(MyService)。这些是 logcat 结果:-
2019-10-04 01:42:20.842 1818-1922/com.example.android.sunshineweatherapp D/noor: i: 1
2019-10-04 01:42:21.843 1818-1922/com.example.android.sunshineweatherapp D/noor: i: 2
2019-10-04 01:42:22.845 1818-1922/com.example.android.sunshineweatherapp D/noor: i: 3
2019-10-04 01:42:23.856 1818-1922/com.example.android.sunshineweatherapp D/noor: i: 4
2019-10-04 01:42:24.857 1818-1922/com.example.android.sunshineweatherapp D/noor: i: 5
2019-10-04 01:42:25.859 1818-1922/com.example.android.sunshineweatherapp D/noor: i: 6
2019-10-04 01:42:26.860 1818-1922/com.example.android.sunshineweatherapp D/noor: i: 7
2019-10-04 01:42:27.861 1818-1922/com.example.android.sunshineweatherapp D/noor: i: 8
2019-10-04 01:42:28.862 1818-1922/com.example.android.sunshineweatherapp D/noor: i: 9
2019-10-04 01:42:29.863 1818-1922/com.example.android.sunshineweatherapp D/noor: i: 10
根据文档,这不应该是预期的行为:-
标准 AlarmManager 警报(包括 setExact() 和 setWindow()) 推迟到下一个维护时段。
所以我也期待相反的情况(因为如果设备处于打盹模式,则不应触发 setExact())。 我什至在真实设备(运行 Android Marshmallow)上对此进行了测试,得到了相同的结果。
这是一个错误吗?还是我错过了什么?
以下问题可能重复,但未按要求给出答案:- Android M (preview) Doze mode and AlarmManager
PS:-
这是我第三次在这个平台上发布这个问题,因为我没有得到任何答案。我在整个互联网(reddit、androidcentral、quora、coderanch)上都找不到任何帮助。
我必须制作一个闹钟应用程序,如果此问题仍然存在,我将无法正确测试行为。
【问题讨论】:
-
你有没有试过在真机上测试这个案例?
-
@BenShmuel 是的,我在运行 android 6 的 HTC Desire 上对其进行了测试,但得到了相同的结果......令人惊奇的是,作业调度程序没有在打盹模式下运行(如预期的那样),但警报管理器 setexact () 做到了!
标签: java android alarmmanager emulation android-doze