0

I am working on an alarm application. Users can set alarms in the application and some actions have to do when one of the alarms triggered. I used the first alarm manager set method. But only the first alarm worked right time. Others triggered after almost 5 minutes of delayed.

Intent intent = new Intent(applicationContext, OneShotAlarm.class);
            PendingIntent sender = PendingIntent.getBroadcast(applicationContext, 0,
                    intent, FLAG_ONE_SHOT);    
AlarmManager alarmManager = (AlarmManager) applicationContext.getSystemService(ALARM_SERVICE);
alarmManager.cancel(sender);
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmCalendar.getTimeInMillis(), sender);

Later, I found this. I changed set method to setexact method and all alarms looked like working perfectly.

alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmCalendar.getTimeInMillis(), sender);

But I saw sometimes still triggered with delay. Something like 5-8 minutes.

Where is my wrong?

UPDATE:

I kept log files when I first detect the error. I saw delay times in log files. Some of pre setexact method logs like below:

16:30:59 Next alarm set to 24/05/2020 16:35:00

16:35:00 Alarm triggered

16:35:00 Notification Sent

16:35:00 Next alarm set to 24/05/2020 16:40:00

16:44:47 Alarm triggered

16:44:47 Notification Sent

16:44:47 Next alarm set to 24/05/2020 16:50:00

16:54:48 Alarm triggered

16:54:48 Notification Sent

16:54:48 Next alarm set to 24/05/2020 18:00:00

And some of post setexact method logs like below:

03:00:00 Alarm triggered

03:00:00 Notification Sent

03:00:00 Next alarm set to 25/05/2020 07:00:00

07:08:14 Alarm triggered

07:08:15 Notification Sent

07:08:15 Next alarm set to 25/05/2020 08:30:00

08:34:47 Alarm triggered

08:34:47 Next alarm set to 25/05/2020 09:00:00

09:00:00 Alarm triggered

09:00:00 Next alarm set to 25/05/2020 18:00:00

UPDATE: My alarm set code is inside of a method. I call it after every data update or triggered alarm. This way, I make sure always the next alarm can be set.

Community
  • 1
  • 1
Anıl Köksal
  • 23
  • 2
  • 6

1 Answers1

0

For current Android version, you should recall alarm manager again in onReceive. This is the example of Alarm Manager called every 5 second :

public class TestApplication extends Application {
         private static Context context;

         @Override
         public void onCreate() {
             super.onCreate();

             context = getApplicationContext();
         }

         static void createAlarm() {
             Intent intent = new Intent(context, AlarmReceiver.class);
             PendingIntent pendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, 0);
             AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
             alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, pendingIntent);
         }
}

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Alarm Manager Test", Toast.LENGTH_SHORT).show();
        TestApplication.createAlarm();
    }
}
andika_kurniawan
  • 511
  • 4
  • 9
  • 20
  • First, I am sorry for did not mention that. But I thought not connected to my issue. My code is in a method and I call it after every update of data and receive an alarm. So we can say I already did it. Second, this may be a solution if my issue was not triggered more than once. But alarms triggered every time. Just sometimes triggered with delay. I am gonna update my question for clearance. Anyway thanks for answer – Anıl Köksal May 25 '20 at 19:04