This is my code. I think this method is right but I am not able to show the notification at exact time. There is always a delay of a few minutes.
public void setSimpleAlarm() {
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 10);
c.set(Calendar.MINUTE, 19);
c.set(Calendar.SECOND, 40);
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(getActivity().ALARM_SERVICE);
Intent intentAlarm = new Intent(getActivity(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 3, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
Well, you're using setInexactRepeating(). As the name suggest and the [documentation](https://developer.android.com/reference/android/app/AlarmManager.html#setInexactRepeating(int, long, long, android.app.PendingIntent)) confirms it's for a "repeating alarm that has inexact trigger time requirements".
The documentation for [setRepeating()](https://developer.android.com/reference/android/app/AlarmManager.html#setRepeating(int, long, long, android.app.PendingIntent)) contains the answer:
Note: as of API 19, all repeating alarms are inexact. If your
application needs precise delivery times then it must use one-time
exact alarms, rescheduling each time as described above. Legacy
applications whose targetSdkVersion is earlier than API 19 will
continue to have all of their alarms, including repeating alarms,
treated as exact.
Related
Muslims prayer times are 5 time a day , so i want my app to always check time , and when the current time matches with one from the 5 time, the App trigger an Action like play a media file , or send A notification to the userŅ
Use AlarmManager
AlarmManager mAlarmManger = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
//Create pending intent & register it to your alarm notifier class
Intent intent = new Intent(this, AlarmReceiver_maandag_1e.class);
intent.putExtra("uur", "1e"); // if you want
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
//set timer you want alarm to work (here I have set it to 9.00)
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
//set that timer as a RTC Wakeup to alarm manager object
mAlarmManger.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Thanks to zipo13 for the correction. As GoogleDeveloper say
Note: Beginning with API 19 (Build.VERSION_CODES.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.
I am attempting to create a sample alarm application using AlarmManager. However, the alarm goes off at different times even though I have set a specific time for it to be triggered.
Setting the alarm:
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 18); // trigger at 6 PM
Intent notifyIntent = new Intent(context, CheckupAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast
(context, 100, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
android.app.AlarmManager alarmManager = (android.app.AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (alarmManager != null) {
alarmManager.setInexactRepeating(android.app.AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
android.app.AlarmManager.INTERVAL_DAY, pendingIntent);
}
Manifest:
<receiver
android:name=".notifications.CheckupAlarmReceiver"
android:enabled="true"
android:exported="false" />
To re-iterate, the alarm works fine. It just fires at the wrong time. Is the Calendar instance not taking the time zone in to consideration? What am I doing wrong here?
Your are setting setInexactRepeating() which as its name says, it could not be fired at the exact time you specified. This is an optimization so the Operating System tries to get more than one alarm which should fire at similar times to fire all of them at the same time.
Instead, you can use void setExactAndAllowWhileIdle(int type, long triggerAtMillis, PendingIntent operation). See documentation.
Example:
alarmManager.setExactAndAllowWhileIdle(android.app.AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
As its recommended, you shouldn't use a repeating alarm with exact times. Instead, you can wait until the exact alarm fires and then schedule another alarm for the next day.
For API lower than 23, you can use void setExact(int type, long triggerAtMillis, PendingIntent operation). And for API lower than 19, you can use void setRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation) with the expected exact firing as always.
I want to show toast at a specific time using AlarmManger but my toast is not shown at given time? Help me.
My code is as follows:
private void startAlarm() {
Calendar cal=Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH,9);
cal.set(Calendar.MONTH,7);
cal.set(Calendar.YEAR,2015);
cal.set(Calendar.HOUR_OF_DAY,2);
cal.set(Calendar.MINUTE,55);
cal.set(Calendar.AM_PM,Calendar.PM);
Intent intent = new Intent(this, WelcomActivity.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, 0);
AlarmManager alarmManager = (AlarmManager)getApplicationContext().getSystemService(ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
Toast.makeText(this, "Alarm worked.", Toast.LENGTH_LONG).show();
}
The documentation for setInexactRepeating states:
Your alarm's first trigger will not be before the requested time, but it might not occur for almost a full interval after that time.
[edit thanks to ci_]
Note: as of API 19, all repeating alarms are inexact. If your
application needs precise delivery times then it must use one-time
exact alarms, rescheduling each time as described above. Legacy
applications whose targetSdkVersion is earlier than API 19 will
continue to have all of their alarms, including repeating alarms,
treated as exact.
So you need to use one-time exact alarms and reschedule as described in the documentation.
I want AlarmManager to be fired at 10:30 AM daily based on user mobile device time. It fires at 10:30 AM for sure but the problem is that after 10:30 AM, It repeats without time like after at every half hour or after any unusual time interval.
How to prevent this problem ? I am calling this on Successfull Login and Registration ButtonCick() Event. I also want to stop this if user Logout.
My code is as below :
Intent myIntent = new Intent(Register.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Register.this,
0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar firingCal = Calendar.getInstance();
Calendar currentCal = Calendar.getInstance();
firingCal.set(Calendar.HOUR_OF_DAY, 10);
firingCal.set(Calendar.MINUTE, 30);
firingCal.set(Calendar.SECOND, 0);
long intendedTime = firingCal.getTimeInMillis();
long currentTime = currentCal.getTimeInMillis();
if (intendedTime >= currentTime) {
WakeLocker.acquire(getApplicationContext());
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, intendedTime,
AlarmManager.INTERVAL_DAY, pendingIntent);
WakeLocker.release();
} else {
WakeLocker.acquire(getApplicationContext());
firingCal.add(Calendar.DAY_OF_MONTH, 1);
intendedTime = firingCal.getTimeInMillis();
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, intendedTime,
AlarmManager.INTERVAL_DAY, pendingIntent);
WakeLocker.release();
}
Code seems fine. If the target version is 19,
Note:
as of API 19, all repeating alarms are inexact. If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above. Legacy applications whose targetSdkVersion is earlier than API 19 will continue to have all of their alarms, including repeating alarms, treated as exact.
Source:
http://developer.android.com/reference/android/app/AlarmManager.html#setRepeating(int,long,long,android.app.PendingIntent)
Note:
Beginning in API 19, the trigger time passed to this method is treated as inexact: the alarm will not be delivered before this time, but may be deferred and delivered some time later. The OS will use this policy in order to "batch" alarms together across the entire system, minimizing the number of times the device needs to "wake up" and minimizing battery use. In general, alarms scheduled in the near future will not be deferred as long as alarms scheduled far in the future.
With the new batching policy, delivery ordering guarantees are not as strong as they were previously. If the application sets multiple alarms, it is possible that these alarms' actual delivery ordering may not match the order of their requested delivery times. If your application has strong ordering requirements there are other APIs that you can use to get the necessary behavior; see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent).
Applications whose targetSdkVersion is before API 19 will continue to get the previous alarm behavior: all of their scheduled alarms will be treated as exact.
Source:
http://developer.android.com/reference/android/app/AlarmManager.html#set(int,long,android.app.PendingIntent)
Please check if that works for your requirement.
I want to set more than one daily alarm in my android application for that I am making demo code like this
Intent i = new Intent(this, AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i,PendingIntent.FLAG_ONE_SHOT);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE, 1);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 30000, pi);
I has given 30000 ms as a interval, so I think it should be repeat every 30 second. but not repeating. its ringing once after 1 min from I started the app is I am wrong ? and what should I do to set multiple daily alarm in My application ?
Thanks!
I think your problem lies in your PendingIntent with the flag FLAG_ONE_SHOT, so with this you can only set your alarm once. If you want repeating alarm, try using the flag FLAG_UPDATE_CURRENT.
Source: http://developer.android.com/reference/android/app/PendingIntent.html