admin管理员组

文章数量:1188999

I've seen that there is a lot of similar question with people having trouble regarding this topic, however I still couldn't a solution that worked for me. And so here I am..

I am trying to create an alarm feature. It has a usual default behaviour of an alarm - meaning the user can create any amount of alarms, at any time, set to trigger at any time. From reading the docs I found out:

Note: Neither setAndAllowWhileIdle() nor setExactAndAllowWhileIdle() can fire alarms more than once per nine minutes, per app.

The only possible solution that seemed possible was to combine the wakeLock with alarmManager. And that was followingly: The 1st intent would be captured in the doze mode, within the boardcast. Then I would check weather there are more alarms within that 9 minute doze period and if there were I would create a wakeLock till the last alarm of that period was executed. The wakeLock was of type:

powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyApp::AlarmWakeLock");

However while the lock showed as acquired in the logs for some reason the wakeLock would not take effect and wake the device to allow the other alarms to trigger the broadcast. Why is that happening? I had added the required permissions for this action - android.permission.WAKE_LOCK, android.permission.SYSTEM_ALERT_WINDOW, android.permission.SCHEDULE_EXACT_ALARM. And the PendingIntent that is supposed to fire that alarmReceiver is of type: PendingIntent.FLAG_UPDATE_CURRENT and has always the same id with unique extras attached.

I tested this on multiple devices both physical/virtual with different API and the issue seems to persist across.

The last thing I thought would be the battery restriction, however the default clock app on the phone was not excluded from those restriction and still works as supposed so I do not think that play a role either.

Why is this occuring? Am I missing something in the code? Thank you, any help would be much appriciated

本文标签: javaHow to AlarmManager while the device is asleepStack Overflow