2016-03-03 69 views
1

我有一個奇怪的問題,當我編程方式創建日曆事件 它始終注意生日日曆(型)我沒有,爲什麼它注意到這樣的任何線索。Android的創建日曆事件始終作爲生日

我的代碼使用的是如下: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); 

請確實有人有任何建議或想法至極可以點我正確的方向?

在此先感謝。

回答

0

您必須將CalendarId值設置爲3而不是1,這是默認生日日曆。 例如

values.Put(CalendarContract.Events.InterfaceConsts.CalendarId, 1); 

將其更改爲

values.Put(CalendarContract.Events.InterfaceConsts.CalendarId, 3); 

它解決了同樣的問題對我來說。

1

某些設備在生日中使用日曆id = 1,但通常不會。因此,爲了獲得特定設備(對應於具有日曆應用程序配置的電子郵件ID)正確的日曆ID,使用下面的代碼:

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; 
} 

注意點: IS_PRIMARY_COLOUM這在電子郵件ID,而不是時返回1爲假期和節假日。

請參考:https://developer.android.com/reference/android/provider/CalendarContract.CalendarColumns.html#IS_PRIMARY瞭解詳情。

1

Pkosta指向在正確的方向。 你必須採取第一個日曆是VISIBLEIS_PRIMARY

long calId = 0; 
String selection = CalendarContract.Calendars.VISIBLE + " = 1 AND " 
+ CalendarContract.Calendars.IS_PRIMARY + " = 1"; 

Uri calendarUri = CalendarContract.Calendars.CONTENT_URI; 
Cursor cur = cr.query(calendarUri, null, selection, null, null); 

if (cur != null && cur.moveToFirst()) { 
    Get the field values 
    calID = cur.getLong(CalendarContract.Calendars._ID); 
} 
if (cur != null) { 
    cur.close(); 
} 

return calId;