How to avoid alarm trigger at time of set? - android

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

Related

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 trigger alarmManager's initial run after specific duration?

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?

AlarmManager repeating every 60 seconds regardless

Intent intent = new Intent(this, Passive.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 50000,
intent, 0);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + 1000, 3600000, pendingIntent);
That's the code I am using, it originally got the repeat time from a shared setting but even when I hard code it is still repeating every 60 seconds instead of the specified time.
It might be worth mentioning, I am not experiencing this issue on my Tablet, just my HTC One X.
use this one before setting alarm--
PendingIntent pendingIntent = PendingIntent.getService(this, 50000,
intent, PendingIntent.FLAG_NO_CREATE);
//Cancelling the PendingIntent in the AlarmManager If it is already exist
if(pendingIntent != null) {
am.cancel(pendingIntent);
pendingIntent.cancel();
}
The phone required a full uninstall and re install of the application and now the correct behavior is shown.

AlarmManager set to same BroadcastReceiver doesn't work

I want to be able to register two alarms to the same BroadcastReceiver. However, the first alarm never gets fired. How can I make this work?
Calendar now = Calendar.getInstance();
now.set(Calendar.SECOND, now.get(Calendar.SECOND) + 5);
long trigger1 = now.getTimeInMillis();
now.set(Calendar.SECOND, now.get(Calendar.SECOND) + 10);
long trigger2 = now.getTimeInMillis();
Intent startIntent = new Intent(AlarmStartReceiver.START_ALARM);
startIntent.putExtra(AlarmStartReceiver.EXTRA_ALARM_ID, 4);
PendingIntent startPIntent = PendingIntent.getBroadcast(context, 0, startIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent startIntent2 = new Intent(AlarmStartReceiver.START_ALARM);
startIntent2.putExtra(AlarmStartReceiver.EXTRA_ALARM_ID, 5);
PendingIntent startPIntent2 = PendingIntent.getBroadcast(context, 0, startIntent2, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, trigger1, startPIntent);
alarm.set(AlarmManager.RTC_WAKEUP, trigger2, startPIntent2);
Only the second one goes off. How can I make them both go off?
EDIT FOR ANSWER: Set the requestCode to something unique. The second param of the PendingIntent.getBroadcast) method
android pending intent notification problem
Set the requestCode to something unique. The second param of the PendingIntent.getBroadcast) method android pending intent notification problem
Are you looking for separate notification event for each alarm you are setting? Or it has to be same notification with number of alarm events showing on the status bar icon?
Look at this post on how you can use "setData()" to the intent to create separate alarms.
Alarm Manager - Scheduling multiple Non-repeating events

Categories

Resources