I have a TimePicker and a Button and I want to set an alarm when the user clicks on the Button at the time specified.
It gets the hour well when I click on the button but the alarm is being fired sometimes at the specified time, but not always. It is fired at the specified time less times than it does not fired. Most of the times the BroadcastReceiver does not fire at the specified time, it fires with a delay.
This is the code that I have right now:
On Alarm class (code refered to the BroadcastReceiver)
AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
Calendar cal= Calendar.getInstance();
cal.set(Calendar.MONTH,cal.get(Calendar.MONTH));
cal.set(Calendar.YEAR,cal.get(Calendar.YEAR));
cal.set(Calendar.DAY_OF_MONTH,cal.get(Calendar.DATE));
cal.set(Calendar.HOUR_OF_DAY,hour);
cal.set(Calendar.MINUTE,minutes);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 1000 * 60 * 5, alarmIntent);
where hour and minutes are the hour and minutes retrieved from the TimePicker.
On AlarmReceiver class
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("prove", "We enter on BroadcastReceiver");
}
}
On Manifest.xml
<receiver
android:name=".AlarmReceiver"
android:enabled="true" >
</receiver>
For example, I have put a log to see what hour I was retrieving from TimePicker before show the log of BroadcastReceiver. I clicked it at 16:48 (on the TimePicker) and this is the result:
12-06 16:47:02.951 7980-7980/com.project.user.project D/prove: hour: 16:48
12-06 16:50:23.727 7980-7980/com.project.user.project D/prove: We enter on BroadcastReceiver
12-06 17:00:01.070 7980-7980/com.project.user.project D/prove: We enter on BroadcastReceiver
As you can see, the BroadcastReceiver has been fired two times at 16:50 and 17:00 when I suppose that they should fire at 16:48 and 16:53 because I have set that it will be a delay of 5 minutes between them, not 10 minutes as in this case.
So, what can I do to fire the alarm at the exact time and not fire it with a delay? Am I missing some configuration?
Thanks in advance!
As mentioned in the documentation, setRepeating is inexact.
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, to fire the alarm at the exact time as of API 19, use setExact, and inside each alarm set the next alarm.
Schedule an alarm to be delivered precisely at the stated time.
Use setInexactRepeating
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(), 1000 * 60 * 5, alarmIntent);
And you can check my code what I did in my app.I worked perfect.
pendingIntent = PendingIntent.getBroadcast(this, _id, myIntent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, timeToAlarm, AlarmManager.INTERVAL_DAY, pendingIntent);
May it help..
Related
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 call a BroadcastReceiver after specific interval of time. BroadcastReceiver is calling but it is not calling exactly after 10 seconds sometimes it call is 20 sec sometimes more than 20 seconds. I want to call BroadcastReceiver exactly after 10 seconds
This is the code of MainActivity
Intent myIntent = new Intent(MainActivity.this, SampleReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, wakeupTime,1000 * 10, pendingIntent);
This is code of broadcastreceiver
public class SampleReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.e("Brodcast","receiver");
}
}
This is my log file
09-05 14:48:21.444 18222-18222/com.loconav.partners.tracker E/Brodcast: receiver
09-05 14:49:21.509 18222-18222/com.loconav.partners.tracker E/Brodcast: receiver
09-05 14:50:31.578 18222-18222/com.loconav.partners.tracker E/Brodcast: receiver
09-05 14:51:21.618 18222-18222/com.loconav.partners.tracker E/Brodcast: receiver
From the documentation of AlarmManager:
Note: 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.
Exact repeating is not possible currently with a single set of an alarm on API level 19+.
If you would like your repeating alarm to fire exactly when you want it, then you should use setExact() (or setExactAndAllowWhileIdle() according to your needs) and reschedule it every time it fires.
I don't know whats in wakeupTime.
The code works for me as
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),1000*10,pendingIntent);
First, use ELAPSED_REALTIME_WAKEUP type, because you don't need fire alarm at a specific time of day. When you use rtc time, it can be updated by the system - when it syncs with time server, and your alarm could be never fire up at all.
Second, do not use BroadcastReceivers defined as subclasses of Activity - they should be set up in AndroidManifest.xml, because if your activity is destroyed - you will never receive your Intent.
I thinking you have to provide unique request code to get broadcast method.
Intent myIntent = new Intent(MainActivity.this, SampleReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, uniqueRequestCode, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, wakeupTime,1000 * 10, pendingIntent);
uniqueRequestCode could be the System.currentTimeInMilliSecond
I want to trigger a daily alarm in my application. I can see the alarm being triggered daily at the correct time for 2 or 3 days but it does not trigger after that. For example if I set alarm to trigger at 08:00 AM, it will trigger at 8 AM daily for 2 or 3 days and after that there is no alarm triggered. There is no app crashing or anything, it simply does not trigger. I have a BroadcastReceiver registered (in AndroidManifest.xml) for this alarm and i can see logs being printed daily at the correct time but only for 2 or 3 days. After that there is no activity and the app just seems to die down.
Please find my code below :
final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
final long intervalDay = 60*60*24*1000L;
final long alarmTime = calendar.getTimeInMillis();
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, intervalDay, pendingIntent);
I have also used alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, AlarmManager.INTERVAL_DAY, pendingIntent); but it did not make any difference (didnt expect it to make any though).
I do not want to use alarmManager.setInexactRepeating() as it does not trigger the alarm at exact time but there is slight delay.
Any help appreciated !!
Thanks.
That's because android cannot hold the alarm content for more than 2-3 days. Though there is no sure shot solution that i know.
I fixed it by cancelling and resetting alarm everytime alarm is triggered.
Something like this.
Instead of using setRepeat use set or setExact to trigger for once:
if(android.os.Build.VERSION.SDK_INT<Build.VERSION_CODES.KITKAT) {
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmtime, pendingIntent);
} else {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmtime, pendingIntent);
}
Then in onRecieve() method of your alarmReciever after performing your task, reset the alarm again for next followed by cancelling all pending intents:
PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), reminderModal.get_remindID(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
pendingIntent.cancel();
Hope it helps.
I'm new to android and using alarmManager and I was wondering if there is a way to set an alarm in android that triggers for example every monday until a certain specific date. Like this :
Start date 10/09/15
Remind me something every monday at 2:30 pm
Until
End date 11/09/15
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 14);
calendar.set(Calendar.MINUTE, 30);
int weekInMillis = 7 * 24 * 60 * 60 * 1000;
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
weekInMillis, PendingIntent.getBroadcast(context, 0, new Intent(context, ReminderAlarmWakefulBroadcastReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT));
Above code snippet sets an alarm for 2:30 PM that repeats itself every week. Tweak calendar for varying the time at which the alarm goes off. For example, the coming Monday.
When the alarm goes off, it sends a broadcast which will be received by ReminderWakefulBroadcastReceiver, a custom receiver containing the code that you want to run every Monday at 2:30 PM. This code should also check whether it is time to cancel the alarm and if it is, the following code cancels it:
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(PendingIntent.getBroadcast(context, 0, new Intent(context, ReminderAlarmWakefulBroadcastReceiver.class));
References:
AlarmManager, Scheduling Repeating Alarms, PendingIntent
If you know how to setup an Alarm, the solution is quite simple:
1) At the time you setup the Alarm, calculate the maximum timestamp you want it to run, and save it as a local preference.
2) Then in the Alarm code itself, each time it is triggered you can make a first test to see if the current timestamp is before or after your limit preference saved at first time.
3) If reached, then cancel the Alarm as #karthik said. If not, keep your code going...
I am creating a clock application that has alarm feature too. The time is showing up properly and I am also setting multiple alarm properly.
I am creating multiple alarm using different id and also saving the same into Database so that I can view the list of alarms in a listview. Now I am trying to set ON and OFF functionality for my alarm. I have a problem there.
On itemclick if alarm is ON it switches OFF with the help of:
Intent intent = new Intent(Main.this,TaskRecieverForAlarm.class);
PendingIntent pi = PendingIntent.getBroadcast(Main.this, Integer.parseInt(cont[0]), intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.cancel(pi);
The above code cancels the alarms perfectly fine.
To switch ON the alarm I am using:
Intent intent = new Intent(Main.this, TaskRecieverForAlarm.class);
intent.putExtra("AlarmDate", cont[1]);
intent.putExtra("key", Integer.parseInt(cont[0]));
PendingIntent sender = PendingIntent.getBroadcast(Main.this, Integer.parseInt(cont[0]) , intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
if(type.equalsIgnoreCase("daily"))
{
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 1440*60000 ,sender);
}
else if(type.equalsIgnoreCase("weekly"))
{
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender); am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 7*1440*60000 ,sender);
}
Now as soon as I click the OFF to ON, alarm triggers and calls the TASKReceiverFORAlarm (broadcast receiver) even though the alarm time is 4 or 5 hours from the current time. I am not sure where I am going wrong?
Can somebody help me out?
Thanks!
I think I found the answer here:
public void setRepeating (int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)
Added in API level 1
Schedule a repeating alarm. Note: for timing operations (ticks, timeouts, etc) it is easier and much more efficient to use Handler. If there is already an alarm scheduled for the same IntentSender, it will first be canceled.
Like set(int, long, PendingIntent), except you can also supply a rate at which the alarm will repeat. This alarm continues repeating until explicitly removed with cancel(PendingIntent). If the time occurs in the past, the alarm will be triggered immediately, with an alarm count depending on how far in the past the trigger time is relative to the repeat interval.
If an alarm is delayed (by system sleep, for example, for non _WAKEUP alarm types), a skipped repeat will be delivered as soon as possible. After that, future alarms will be delivered according to the original schedule; they do not drift over time. For example, if you have set a recurring alarm for the top of every hour but the phone was asleep from 7:45 until 8:45, an alarm will be sent as soon as the phone awakens, then the next alarm will be sent at 9:00.
If your application wants to allow the delivery times to drift in order to guarantee that at least a certain time interval always elapses between alarms, then the approach to take is to use one-time alarms, scheduling the next one yourself when handling each alarm delivery.
Parameters
type One of ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP}, RTC or RTC_WAKEUP.
triggerAtMillis time in milliseconds that the alarm should first go off, using the appropriate clock (depending on the alarm type).
intervalMillis interval in milliseconds between subsequent repeats of the alarm.
operation Action to perform when the alarm goes off; typically comes from IntentSender.getBroadcast().
The way you use that function is:
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 1440*60000 ,sender);
Try this one:
//The only variable here is the desired hour of the alarm, which
// has to be obtained in milliseconds
long alarmSetAt = // The hour of the Alarm for the current date in milliseconds
long time = cal.getTimeInMillis() - alarmSetAt;
if(time > 0){
time = -time + cal.getTimeInMillis();
}
else{
time = time + cal.getTimeInMillis() + 1440*60000;
}
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 1440*60000 ,sender);