Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Create Calendar Event always as Birthday

I have a strange issue when I create a calendar event programmatically its always noted as Birthday Calendar (type) I don't have any clue why its noted like that.

My code I use is as below: Xamarin C#

ContentResolver cr = ((Activity)Forms.Context).ContentResolver;
ContentValues values = new ContentValues();
String eventUriString = "content://com.android.calendar/events";

//Insert Events in the calendar...
values.Put(CalendarContract.Events.InterfaceConsts.CalendarId, 1);
values.Put(CalendarContract.Events.InterfaceConsts.Title, title);
values.Put(CalendarContract.Events.InterfaceConsts.Status, 1);
values.Put(CalendarContract.Events.InterfaceConsts.Description, description);
values.Put(CalendarContract.Events.InterfaceConsts.Dtstart, GetDateTimeMS(year, month, day, hour, minute));
values.Put(CalendarContract.Events.InterfaceConsts.Dtend, GetDateTimeMS(year, month, day, hour, minute));
values.Put(CalendarContract.Events.InterfaceConsts.AllDay, allday ? "1" : "0");
values.Put(CalendarContract.Events.InterfaceConsts.HasAlarm, hasalarm ? "1" : "0");
values.Put(CalendarContract.Events.InterfaceConsts.EventColor, Android.Graphics.Color.Green);
values.Put(CalendarContract.Events.InterfaceConsts.EventTimezone, "GMT+" + zone + ":00");
values.Put(CalendarContract.Events.InterfaceConsts.EventEndTimezone, "GMT+" + zone + ":00");
cr.Insert(Android.Net.Uri.Parse(eventUriString), values);

Please does someone has any tips or ideas which can point me to the right direction?

Thanks in advance.

like image 938
Stefan van de Laarschot Avatar asked Mar 03 '16 15:03

Stefan van de Laarschot


People also ask

How do I create a birthday event in Google Calendar?

On Android phone: Create an event, e.g., "Fido's Birthday." Then go to that event and tap to open edit. Under the date and time you will see "Does not repeat." Tap that.


1 Answers

Some devices uses calendar id = 1 for birthdays but generally not. So to get the correct calendar id for particular device (corresponding to the email id configured with calendar app), use below code:

private int getCalendarId(Context context){

  Cursor cursor = null;
  ContentResolver contentResolver = context.getContentResolver();
  Uri calendars = CalendarContract.Calendars.CONTENT_URI;

  String[] EVENT_PROJECTION = new String[] {
        CalendarContract.Calendars._ID,                           // 0
        CalendarContract.Calendars.ACCOUNT_NAME,                  // 1
        CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,         // 2
        CalendarContract.Calendars.OWNER_ACCOUNT,                 // 3
        CalendarContract.Calendars.IS_PRIMARY                     // 4
};

  int PROJECTION_ID_INDEX = 0;
  int PROJECTION_ACCOUNT_NAME_INDEX = 1;
  int PROJECTION_DISPLAY_NAME_INDEX = 2;
  int PROJECTION_OWNER_ACCOUNT_INDEX = 3;
  int PROJECTION_VISIBLE = 4;

  cursor = contentResolver.query(calendars, EVENT_PROJECTION, null, null, null);

  if (cursor.moveToFirst()) {
    String calName;
    long calId = 0;
    String visible;

    do {
        calName = cursor.getString(PROJECTION_DISPLAY_NAME_INDEX);
        calId = cursor.getLong(PROJECTION_ID_INDEX);
        visible = cursor.getString(PROJECTION_VISIBLE);
        if(visible.equals("1")){
            return (int)calId;
        }
        Log.e("Calendar Id : ", "" + calId + " : " + calName + " : " + visible);
    } while (cursor.moveToNext());

    return (int)calId;
  }
  return 1;
}

Point to note : IS_PRIMARY_COLOUM which returns 1 in case of email id and not for bithdays and holidays.

Please refer: https://developer.android.com/reference/android/provider/CalendarContract.CalendarColumns.html#IS_PRIMARY for details.

like image 54
Pkosta Avatar answered Nov 02 '22 22:11

Pkosta