0

I am trying to notify my app users with a notification in a specific day in the year and I am stuck in the middle as I did scheduled notifications at a regular bases (daily, weekly,..) and I don't want that.

Here is the code:

  Calendar cal = Calendar.getInstance();              
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", cal.getTimeInMillis());
intent.putExtra("allDay", false);
intent.putExtra("rrule", "FREQ=DAILY");
intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
intent.putExtra("title", "A Test Event from android app");
startActivity(intent);

    private String getCalendarUriBase(Activity act) {

    String calendarUriBase = null;
    Uri calendars = Uri.parse("content://calendar/calendars");
    Cursor managedCursor = null;
    try {
        managedCursor = act.managedQuery(calendars, null, null, null, null);
    } catch (Exception e) {
    }
    if (managedCursor != null) {
        calendarUriBase = "content://calendar/";
    } else {
        calendars = Uri.parse("content://com.android.calendar/calendars");
        try {
            managedCursor = act.managedQuery(calendars, null, null, null, null);
        } catch (Exception e) {
        }
        if (managedCursor != null) {
            calendarUriBase = "content://com.android.calendar/";
        }
    }
    return calendarUriBase; }

    // get calendar
Calendar cal = Calendar.getInstance();     
Uri EVENTS_URI = Uri.parse(getCalendarUriBase(this) + "events");
ContentResolver cr = getContentResolver();

// event insert
ContentValues values = new ContentValues();
values.put("calendar_id", 1);
values.put("title", "Reminder Title");
values.put("allDay", 0);
values.put("dtstart", cal.getTimeInMillis() + 11*60*1000); // event starts at 11 minutes from now
values.put("dtend", cal.getTimeInMillis()+60*60*1000); // ends 60 minutes from now
values.put("description", "Reminder description");
values.put("visibility", 0);
values.put("hasAlarm", 1);
Uri event = cr.insert(EVENTS_URI, values);

// reminder insert
Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(this) + "reminders");
values = new ContentValues();
values.put( "event_id", Long.parseLong(event.getLastPathSegment()));
values.put( "method", 1 );
values.put( "minutes", 10 );
cr.insert( REMINDERS_URI, values );

manifest

<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />

But I want to create a reminder notification in a specific day of the year not at regular bases (daily, weekly, etc). How can I write the code for that?

halfer
  • 19,824
  • 17
  • 99
  • 186
Hamed Baatour
  • 6,664
  • 3
  • 35
  • 47

2 Answers2

2

Use AlarmManager. Something like this:

Calendar cal = Calendar.getInstance();
cal.setTime(...);

Intent i = new Intent(context, YourBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);

AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);
Mike
  • 1,000
  • 1
  • 10
  • 18
  • @Mouyahama please look at this answer: http://stackoverflow.com/questions/6545935/if-i-have-a-specific-date-of-a-day-how-do-i-get-the-date-of-that-day-in-the-pre – Mike Dec 08 '13 at 10:44
  • Also, you could look at how to use the `add()` method of the `Calendar` object. – Mike Dec 08 '13 at 10:45
0

You can use alarmManager service for this purpose!

halfer
  • 19,824
  • 17
  • 99
  • 186
Abhishek Shukla
  • 1,242
  • 8
  • 11