Search code examples
androidalarmschedule

Set a repeating alarm


9I'm making an app that reminds you to take your medicine at some time, so the user gives me the frequency and the period of time, for example:

frequency = 8 hours Period = 7 days

So I'll have make an alarm that triggers every 8 hours for 7 days. This is the code I have now, but it's not working the way I want it... (It is sending all the notifications at the same time, so in the above example it is sending the 21 notifications at once, just a few seconds after I run the code, which means that instead of sending the first one, it is sending all of them)

private void makeAlarm(MedAlarm mAlarm) {

    String medName = mAlarm.getMedName();
    String medDescripcion = mAlarm.getMedDescription();
    String frequency = mAlarm.getMedIntervalHours();
    String duration = mAlarm.getMedIntervalDays();
    int lastitem = ((24 / Integer.parseInt(frequency)) * 
    Integer.parseInt(duration)) - 1;

    String alarmMsg = medName;

    for (int k = 0; k < ((24 / Integer.parseInt(frequency)) * 
         Integer.parseInt(duration)); k++) {// hours

        setAlarm(Integer.parseInt(frequency) * (k + 1), alarmMsg, k + "", 
         "0");
        Log.i("setAlarm", k + " - ");
        Log.i("duration", Integer.parseInt(frequency) * (k + 1) + "");

        if (k == lastitem) {
            setAlarm(Integer.parseInt(frequency) * (k + 1), alarmMsg, k + "", 
            "1");
        }
    }
}

/**
 * @param duration
 * @param name
 * @param id
 * @param lastitem 0 normal item, 1 last item
 */
private void setAlarm(int duration, String name, String id, String lastitem) 
{

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());

    Intent alertIntent = new Intent(this, AlertReceiver.class);
    alertIntent.putExtra("message", name);
    alertIntent.putExtra("id", id);
    alertIntent.putExtra("lastitem", lastitem);
    PendingIntent pi = PendingIntent.getBroadcast(this, duration++, 
    alertIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    int alarmType = AlarmManager.ELAPSED_REALTIME;
    final int FIFTEEN_SEC_MILLIS = 15000;
    final int HOURS_MILLIS = 3600000 * duration;
    AlarmManager alarmManager = (AlarmManager)
            this.getSystemService(this.ALARM_SERVICE);

    alarmManager.setRepeating(alarmType, SystemClock.elapsedRealtime() + 
    FIFTEEN_SEC_MILLIS,
            HOURS_MILLIS, pi);

    Log.i("intent sent", HOURS_MILLIS + " || ");
}

And the class AlertReciver is the one that sends the notification.

if you could help me with this and tell me what am I missing or if I should aproach this in a different way.

Thank you.


Solution

  • Use alarmManager.set() instead of alarmManager.setRepeating().

    Update your setAlarm() method as below:

    private void setAlarm(int duration, String name, String id, String lastitem)
    {
        Intent alertIntent = new Intent(this, AlertReceiver.class);
        alertIntent.putExtra("message", name);
        alertIntent.putExtra("id", id);
        alertIntent.putExtra("lastitem", lastitem);
        PendingIntent pi = PendingIntent.getBroadcast(this, duration++,
                alertIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
        int alarmType = AlarmManager.ELAPSED_REALTIME;
        final int FIFTEEN_SEC_MILLIS = 15000;
        final int HOURS_MILLIS = 3600000 * duration;
        AlarmManager alarmManager = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);
    
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis() + HOURS_MILLIS);
        alarmManager.set(alarmType, calendar.getTimeInMillis() + FIFTEEN_SEC_MILLIS, pi);
    
        Log.i("intent sent", System.currentTimeMillis() + HOURS_MILLIS + " || ");
    }
    

    Hope this will help~