Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android repeating alarm not working

This works fine:

Intent intent = new Intent(HelloAndroid2.this, AlarmReceiver.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(HelloAndroid2.this, 0,
    intent, PendingIntent.FLAG_ONE_SHOT);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (12 * 1000), pendingIntent);

This doesn't work. I hear the alarm only time.

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (12 * 1000), 3 * 1000, pendingIntent);

I have also tried this, no luck:

Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND, 5);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 7000, pendingIntent);

What is the problem?

like image 851
erdomester Avatar asked Apr 14 '26 07:04

erdomester


1 Answers

From the PendingIntent doc for FLAG_ONE_SHOT:

this PendingIntent can only be used once. If set, after send() is called on it, it will be automatically canceled for you and any future attempt to send through it will fail.

So after the pendingIntent is fired the first time, it will be cancelled and the next attempt to send it via the alarm manager will fail

Try using FLAG_UPDATE_CURRENT

like image 169
ccheneson Avatar answered Apr 15 '26 20:04

ccheneson