Background: I have an Android application running on Android 7.1. A reccurring task is set using AlarmManage to perform a task every day at a specific time. code is like below:
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
long startInMs = getComing9AM();
Intent intent = ...; //an intent to be run when alarm time is up.
PendingIntent ScheduledIntent = PendingIntent.getBroadcast(context, id, intent, 0);
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, startInMs, ScheduledIntent);
private long getComing9AM(){
long now = System.currentTimeMillis();
long today = getToday9AM();
return (now < today? today:getTomorrow9AM());
}
private long getToday9AM(){
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTimeInMillis();
}
private long getTomorrow9AM(){
return getToday9AM() + 7 * 24 * 3600 * 1000;
}
- The android device is not a mobile phone, but a small box. Under normal operation, only power cable is plugged with WIFI connectivity.
Problem Description:
- The scheduled task works pretty well every day. After three weeks or more, the task no longer running.
- All other functionality still working, including MQTT client communication, IO operations.
Analysis result:
After checking the pattern of the problem, it is found that around 25 days from last boot up the task will not work. Being a math-geek who is sensitive to number, "25 days" = 2,160,000 second = 2,160,000,000 ms. And this value is very closed to 2^31-1 (Integer.MAX_VALUE), which is equivalent to 24.85 days.
HDMI cable is plugged to the device, the screen shows no input.
Even a new scheduled task is assigned after 25days (thru HTTPS), the task will not be executed.
After a USB device (i tried a mouse) is plugged to the device, Android is wake up (the screen has a display). And the Android has been awakened, the task can be run normally every day until the next 25 days.
using ADB to check the Android setting, I can only find one parameter value is met with Integer.MAX_VALUE. That is (adb shell dumpsys power):
mMaximumScreenOffTimeoutFromDeviceAdmin=2147483647 (enforced=false) Screen off timeout: 2147483647 ms
Based on finding #5, I have tried to set the screen off timeout to 60000 trying to reproduce the problem. But the task can still be run as usual.
It should not be related to Doze mode as the device is always charged and no battery with it.
(New finding) The value of "mWakefulness" in dumpsys power is Asleep after 24.85days. Before 24.85days this value should be Awake.
(New finding) Given that wWakefulness=Asleep arelay, the "mLastSleepTime"=now - (startupTimestamp + 24.85day).
Seek for advice:
I would like to have some direction to further investigate the problem.
I would like to know if there is a way to simulate the waking up event within the application? (as a workaround before finding the root cause).