I'm sure I'm doing something wrong but I can't get the alarm manager to trigger more than once when I setup an alarm doing the following:
AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, ReminderAlarmReceiver.class);
i.setData(Uri.parse(REMINDER_ID + getReminderID() + getTriggerAtTime()));
i.putExtra(REMINDER_ID, reminderID);
i.putExtra(DiaryListItem.DIARY_ID, parentDiaryID);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
mgr.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtTime, AlarmManager.INTERVAL_DAY;, pi);
It seems to trigger the first day but doesn't trigger the next day. The same thing happens when I try to make a weekly or monthly alarm. Is there a better way of setting up long term alarms?
Did you mean to put that semicolon in your .setRepeating? I imagine your run would crash if it were actually still there:
mgr.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtTime, AlarmManager.INTERVAL_DAY;, pi);
should be
mgr.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtTime, AlarmManager.INTERVAL_DAY, pi);
If that was just a typo, does your logcat give any kind of error?
Related
I want to schedule a repeating alarm at user define time. For that i am using following code
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("id", id);
intent.putExtra("ontime", flag_ontime);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP,
SystemClock.elapsedRealtime() +calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
All works fine...my alarm also fire at desired time but in my receiver i always get only default values from intent not passed one
AlarmReceiver.class
long id = intent.getLongExtra("id", -1);
boolean ontime = intent.getBooleanExtra("ontime", false);
here id is always -1 and ontime is false....please help me
well i for now i have solved this by using alarmmanager setExact() method and on each event fire i re-schedule alarm as per its repeating time....looking for batter solution
I have created a pending intent that fires a repeating alarm every minute. The alarm works fine but when I cancel the alarm, it still fires. I have read other posts about this and I made sure that everything is as suggested, mainly to use FLAG_UPDATE_CURRENT and to use the same code for the intent, when creating and when cancelling. Here is the code I use:
Create the alarm:
final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
final Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
c.add(Calendar.MINUTE, 1);
alarmManager.setRepeating(AlarmManager.RTC, c.getTimeInMillis(), 1000 * 60, getIntent(context));
Cancel the alarm:
final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
getIntent(context).cancel();
alarmManager.cancel(getIntent(context));
The getIntent function:
public static PendingIntent getIntent(Context context) {
return PendingIntent.getBroadcast(context, 20, new Intent(ACTION_MINUTE_ALARM), PendingIntent.FLAG_UPDATE_CURRENT);
}
Does anyone know what the problem is?
After a lot of struggle I discovered that there was another service running (different apk) that was also creating exactly the same pendingIntent using a repeating alarm. Removing that service fixed the problem. Therefore, the method described in the original question works. Hope this helps someone.
I am trying to set periodically alarm every 30 minutes, but i dont think it is working. I use below code:
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(MyActivity.this, Alarm.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(MyActivity.this, 0, intent, 0);
long selectedTimeMiliseconds = (long) (TimeUnit.MINUTES.toMillis(30));
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), selectedTimeMiliseconds, alarmIntent);
How to set the second parameter in method setInexactRepeating? Is it better to use setRepeating method?
Inexact is a save battery method, but it is... inexact.
AlarmManager have a problem, if the device is in sleep mode, the intent not start. You should use WakefulBroadCast.
https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html
Try this..
alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), selectedTimeMiliseconds, alarmIntent);
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.
I have a problem regarding Alarm manager in Android.
I have the following code snippet to set an alarm that should be fired each week(once).
// Add the time and set when the notification will be triggered
Calendar setCalendar = item.getDate();
calendar.set(Calendar.MINUTE,setCalendar.get(Calendar.MINUTE)+10080);
//Create a new alarm intent
Intent alarmIntent = new Intent(ApplicationUtils.getApplicationContext(), AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(ApplicationUtils.getApplicationContext(), requestCode, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// Get the AlarmManager service
AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.ELAPSED_REALTIME, sender);
And I have to following problem. When the week is changed, the notifications are coming up and they never stop.
Does anybody have any idea how can I set the calendar so the alarms are triggered once a week?
Thanks, Arkde
You're third parameters in setRepeating is incorrect. It should be an interval between the repeating alarms in milliseconds.
A week would be: 1000 * 60 * 60 * 24 * 7 .
http://developer.android.com/reference/android/app/AlarmManager.html#setRepeating(int, long, long, android.app.PendingIntent)
Check out the Code it help you.
Intent intent_for_every_second = new Intent(Activity.this, Notifier.class);
pendingIntent_for_every_second = PendingIntent.getBroadcast(Activity.this, 0, intent_for_every_second,0);
AlarmManager alarmManager_for_every_second = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager_for_every_second.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 1000, 1000,pendingIntent_for_every_second);