How to trigger alarmManager's initial run after specific duration? - android

I need to trigger an alarmManager's first run at the start of next minute. for example, if the current time is 6:47:40 seconds, then the alarmManager's first run will trigger at 6:48:00 (i.e. 20 seconds later)
To achieve this I need to figure out how many second is left for the current minute to complete. this can be done using this:
Calendar c = Calendar.getInstance();
int secondsTillNextMinute = 60 - c.get(Calendar.SECOND);
Now How can I use this value in the alarmmanager.SetRepeating? My alarmManager looks like this.
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), 60000, createClockTickIntent(context));
private PendingIntent createClockTickIntent(Context context) {
Intent intent = new Intent(CLOCK_WIDGET_UPDATE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
return pendingIntent;
}
I want to use secondsTillNextMinute instead of System.currentTimeMillis() in my SetRepeating code. How do I do that? the 2nd parameter accepts only milisecond values?

Related

AlarmManager alarm is called immediately when trigger time is in the past

Below is the code I am using to create alarms when data is pulled from external API. If the time set is in the past, the alarm goes off as soon as it is set(2 second gap). For example if I set the alarm for 8:00 AM, 10th April at 10:00 AM on 11th April(Past time). It starts the alarm as soon as it is set.
public static final int ALARM_REQUEST_CODE = 1001;
public static AlarmManager alarmManager = (AlarmManager) EHCApplication.getInstance().getApplicationContext().getSystemService(Context.ALARM_SERVICE);
public static Intent alarmIntent = new Intent(EHCApplication.getInstance().getApplicationContext(), AlarmReceiver.class);
public static PendingIntent pendingIntent = PendingIntent.getBroadcast(EHCApplication.getInstance().getApplicationContext(), ALARM_REQUEST_CODE, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
public static void setAlarm(Reminder rm) {
for (ScheduledTime time : rm.getScheduledTime()) {
Bundle bundle = new Bundle();
bundle.putParcelable(Constants.ARGS_SELECTED_MEDICINE, medicine);
alarmIntent.putExtras(bundle);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time.getTimeInMilliseconds(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
}
I am expecting the alarm to go off from next time it hit the time. Where am I making mistake?
This is the expected behavior.
From the documentation of setRepeating() (and other AlarmManager set methods):
If the stated trigger time is in the past, the alarm will be triggered
immediately
If you would like to prevent that happening, then simply do not set alarms with a past trigger time (e.g. check against System.currentTimeMillis() when setting the alarm).
Well, I ran into same problem and after studying I found that alarm will be run as soon when past time is set for the alarm.
Source: Here is documentation of Alarm Manager - setRepeating()
So, I resolved the issue by checking if "Calendar time is in past from system time than I add a day"
Working code:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, min);
calendar.set(Calendar.SECOND, 0);
alarmManager.cancel(pendingIntent);
// Check if the Calendar time is in the past
if (calendar.getTimeInMillis() < System.currentTimeMillis()) {
Log.e("setAlarm","time is in past");
calendar.add(Calendar.DAY_OF_YEAR, 1); // it will tell to run to next day
}
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, id, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); //Repeat every 24 hours

Android AlarmManager triggered immediately even with correct parameters?

I am setting an AlamManager from the onCreate of my main Activity.
Here is the method
public void scheduleAdsUpdateAlarm() {
long THEE_HOURS = 3 * 60 * 60 * 1000;
long THREE_MINUTES= 3*60*1000;
long UNTIL_FIRST_TRIGGER = THREE_MINUTES;
// Construct an intent that will execute the AlarmReceiver
Intent intent = new Intent(getApplicationContext(), AdsUpdateAlarmReceiver.class);
// Create a PendingIntent to be triggered when the alarm goes off
final PendingIntent pIntent = PendingIntent.getBroadcast(this, AdsUpdateAlarmReceiver.REQUEST_CODE,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
if(alarm != null){
alarm.cancel(pIntent);
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, UNTIL_FIRST_TRIGGER,
THREE_MINUTES, pIntent);
}
}
As you can see the alarm is set to run every three hours with the initial start after three minutes.
Problem is that the alarm goes off immediately when the onCreate and the following alarm setup is called. I don't understand what I am doing wrong?
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, UNTIL_FIRST_TRIGGER,
THREE_MINUTES, pIntent);
should be changed to:
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + UNTIL_FIRST_TRIGGER, THREE_MINUTES, pIntent);
since the second parameter in alarm.setInexactRepeating isn't a milliseconds from now value but an actual time value.

Alarm Manager not scheduling alarm properly

I am setting alarm usng this
Calendar now = Calendar.getInstance();
Calendar alarm = Calendar.getInstance();
alarm.set(Calendar.HOUR_OF_DAY,21);
alarm.set(Calendar.MINUTE,30);
if (alarm.before(now)) {
alarm.add(Calendar.DAY_OF_MONTH, 1); //Add 1 day if time selected before now
}
AlarmManager alarmManager =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context,Receiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context,(int)alarm.getTimeInMillis(),i,0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, (int)alarm.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pi);
But even if I schedule it for next day,it triggers immediately after saving alarm.
Dont know what the issue is have searched a lot but everyone else gets it working
You are casting a long timestamp to int thus losing bits and changing the actual timestamp value. You end up with a time that already has passed so it executes the intent immediately.
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarm.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
Notice that I removed the (int) cast in the last line.

How to avoid alarm trigger at time of set?

I am setting alarm with an interval of 2 hours. It work very perfect but the problem is, at the time of set it trigger the alarm.
It don't have to trigger alarm at time of set rest is all ok.
Intent _myIntent = new Intent(activity, MyReceiverStartPush.class);
_myIntent.putExtra("msg", "Feed");
PendingIntent _myPendingIntent = PendingIntent.getBroadcast(
activity, 0, _myIntent, 0);
AlarmManager _myAlarmManager = (AlarmManager) activity
.getSystemService(Service.ALARM_SERVICE);
_myAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), (AlarmManager.INTERVAL_HOUR)
* 2, _myPendingIntent);
Instead of using System.currentTimeMillis() (which is the reason) you should use System.currentTimeMillis() + 2H there

Android: CountDownTimer vs AlarmManager

I have to run a piece of code every 5 minutes in my service. Will countdowntimer be killed since the app is not in foreground. If this is the case will alarmanager be better in running the code?
Thanks,
Sahil
I think especially if your code runs in a Service that an AlarmManager is the better alternative. You can use the AlarmManager to start your Service in a 5 minute interval like this:
Calendar calendar = Calendar.getInstance();
Intent intent = new Intent(context, YourService.class);
// Set action of Intent or add extras
long period = 5 * 60 * 1000; // 5 minutes
PendingIntent pendingIntent = PendingIntent.getService(context, alarmId, intent, 0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), period, pendingIntent);
If you want to stop your alarm you have to use the same alarmId so it would be best if you put that id in a constant. Also note that you have to restart your alarm every time the phone reboots!

Categories

Resources