Search code examples
javaandroidalarmmanagerandroid-calendar

Alarm manager Calendar


Question about alarm manager I have this code

Calendar cal = Calendar.getInstance();
    cal.add(Calendar.SECOND, 5);
     Integer prof=t.getProfile();
    String prof2=prof.toString();
    Intent intent = new Intent(this, AlarmActivity.class);
    intent.putExtra("prof",(String)prof2);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,(int)t.getId(), intent, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager am =
            (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
            pendingIntent);

Its unfinished yet... I know I can set time of the calendar with cal.set(Calendar.MINUTES,minutes); and same for hours..

But how do I set day? For example - monday? day_of_week sets it? If so - range is 0-6 or 1-7? And lowest value is monday or sunday?

Also, if Im going to make repeating event (once a week) - should I make new calendar and set day of week/hours/minutes? or should I user getInstance() and change hour/min/day of week?

About alarm manager. When u make an alarm, you give request_code which should be uniq. If I reboot my phone - does all the request codes stay in alarm manager on phone? If no - how to make they stay... If yes - how do I delete unnecesery made ones while testing?


Solution

  • You can also use:

    cal.add(Calendar.DATE, 7) 
    

    to set a calendars time to one week from the current calendars setting (and subtract and so forth).

    I think a link to the docs is probably warranted here:

    http://developer.android.com/reference/java/util/Calendar.html

    Regarding your second question, no alarm managers do not persist on phone reboot, you have to save them in shared prefs or SQL and then reload the alarms the next time the phone (and your app) restarts...

    To cancel an alarm you use alarm.cancel(pendingIntent). The pendingIntents you need to keep track of on your own.