Search code examples
javaandroidalarmmanager

AlarmManager not repeating correctly


I try to have a notification repeated daily (For debugging I set it to every 10s). However, it is firing the notification only the first time, then nothing happens.

Here is the code where the alarm is set:

Intent myIntent = new Intent(ctx , NotifyService.class);
AlarmManager alarmManager =(AlarmManager)ctx.getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(ctx, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Calendar calendar = Calendar.getInstance();
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), 1000 * 10, pendingIntent); 

and here is the service:

public class NotifyService extends Service {
   public NotifyService() {

   }

   @Override
   public void onCreate(){
       //Create and Emit the notification.
   }

I have tried different flags in getService(ctx, int, Intent, flags), to use setInexactRepeating and to set a new alarm after every call to the NotifyService.


Solution

  • Use the method below to repeating the alarm once in a day and you have to register broadcast receiver instead of service with AlarmManager so that you can start your service from the receiver and that is recommended.

    Find the official doc.

         private final static String ACTION = "ACTION_ALARM";
         public static void setWakeUpAction(Context context, String hourSet, String minuteSet, String periodSet, int requestCode, String currentAction) {
                try {
                    String mHour = hourSet;
                    String mMin = minuteSet;
                    String[] parsedFormat = null;
                    Calendar calendar = Calendar.getInstance();
                    SimpleDateFormat displayFormat = new SimpleDateFormat("HH:mm");
                    SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mm a");
                    Date date = parseFormat.parse(mHour + ":" + mMin + " " + periodSet);
                    parsedFormat = displayFormat.format(date).split(":");
                    mHour = parsedFormat[0];
                    mMin = parsedFormat[1];
                    calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(mHour));
                    calendar.set(Calendar.MINUTE, Integer.parseInt(mMin));
                    calendar.set(Calendar.SECOND, 00);
                    Intent myIntent = new Intent(context, MyReceiver.class);
                    myIntent.putExtra(ACTION, currentAction);
                    myIntent.putExtra("Time", new String[]{mHour, mMin, periodSet});
                    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCode, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
                    alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
                            AlarmManager.INTERVAL_DAY, pendingIntent);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
    
            }
    

    BroadCastReceiver

    public class MyReceiver extends BroadcastReceiver {
    
        private final String ACTION = "ACTION_ALARM";
        private String ACTION_ONE = "ALARM_REPEAT";
    
        @Override
        public void onReceive(Context context, Intent intent) {
           try {
                String action = intent.getStringExtra(ACTION);
                new ShowToast(context, action);
                if (action.length() > 1) {
                    if (action.equals(ACTION_ONE) ) {
                      String time[] = intent.getStringArrayExtra("Time");
                      startService(context, action);
                    }
                }
            } catch (Exception e) {
            }
    
        }
    
        public void startService(Context context, String action) {
            Intent service1 = new Intent(context, NotifyService.class);
            service1.putExtra(ACTION, action);
            context.startService(service1);
        }
    
    }
    

    Manifest

       <service
            android:name=".NotifyService"
            android:enabled="true" />
    
        <receiver android:name=".MyReceiver" />
    

    Usage

    setWakeUpAction(context, "11", "00","AM","0", "ALARM_REPEAT");