Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 wich can point me to the right direction ?

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
113 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...