setExactAndAllowWhileIdle() for alarmmanager is not working properly
I've found the solution for my problem so, i am posting my own answer here which worked for me.
Using setAlarmClock() method has solved my problem. If you set alarm using setAlarmClock() method then this will not allow the system to go into doze mode before 1 hour of your alarm's time. I had tested this by manually forcing my device to go into doze mode after setting my alarm. Let me explain full scenario.
- First of all i set alarm after 5 minutes from current time and then tried to putting my device into doze mode manually by using following command.
adb shell dumpsys deviceidle force-idle
It Shows
unable to enter into doze mode
- After that i set alarm after 1 hour and 1 minute from current time and then i have tried to put my device into doze mode and it enters into doze mode successfully. Then i have done nothing on my device and it fires alarm exactly on time even it was in a doze mode.
Hence, i conclude that setAlarmClock() method prevents your device from entering to doze mode if there is a small amount of timestamp between current time and your alarm time. Otherwise if your device is already in doze mode then it will gets exit from doze mode before some time of your alarm so, your alarm works fine.
Updated code:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
alarmManager.setAlarmClock(new AlarmManager.AlarmClockInfo(d.getTime(),pendingIntent),pendingIntent);
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
alarmManager.setExact(AlarmManager.RTC, d.getTime(), pendingIntent);
else
alarmManager.set(AlarmManager.RTC, d.getTime(), pendingIntent);
AlarmManager setExact with WakefulBroadcastReceiver sometimes not exact
This behaviour is added in API 19:
Beginning with API 19 (KITKAT) alarm delivery is inexact: the OS will shift alarms in order to minimize wakeups and battery use. There are new APIs to support applications which need strict delivery guarantees; see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent). Applications whose targetSdkVersion is earlier than API 19 will continue to see the previous behavior in which all alarms are delivered exactly when requested.
from AlarmManager.
Important: setExact()
still does not have to be exact, as the docs state:
The alarm will be delivered as nearly as possible to the requested trigger time.
Related Topics
Difference Between Build.Gradle (Project) and Build.Gradle (Module)
Mediastore - Uri to Query All Types of Files (Media and Non-Media)
Parcelable Encountered Ioexception Writing Serializable Object Getactivity()
How to Add Action Bar Options Menu in Android Fragments
Avoiding Content Type Issues When Downloading a File via Browser on Android
Android:Static Variable Null on Low Memory
Get Android Google Analytics Referrer Tag
Android Ndk: Link Using a Pre-Compiled Static Library
"No Resource Identifier Found for Attribute 'Showasaction' in Package 'Android'"
Overridependingtransition Does Not Work When Flag_Activity_Reorder_To_Front Is Used
Android Location Manager, Get Gps Location ,If No Gps Then Get to Network Provider Location
Android Fragment Onrestoreinstancestate
Understanding Android's <Layer-List>
How to Capture an Image in Background Without Using the Camera Application
How to Set Different Applicationid for Each Flavor Combination Using Flavordimensions