在android中向日历添加事件时应该使用CALENDAR_ID?

问题描述:

这是我的代码,我想知道“CalendarContract.Events.CALENDAR_ID”的值是什么?此代码在Pre Android N Os上工作,但是在Nougat中,如果我将“1”作为值,它会在特定字段(提醒,生日,假日等)中创建事件,并且它会根据用户而变化。因此,如果我在某些牛轧糖设备中将“5”作为值传递,那么我可能会在一台设备上获得正确的输出,但在另一部手机中,可能会使用提醒或假期创建事件并在几秒钟内自动删除。在android中向日历添加事件时应该使用CALENDAR_ID?

 ContentResolver cr = context.getContentResolver(); 
     ContentValues values = new ContentValues(); 
     values.put(CalendarContract.Events.DTSTART, startMillis); 
     values.put(CalendarContract.Events.DTEND, endMillis); 
     values.put(CalendarContract.Events.TITLE, title); 
     values.put(CalendarContract.Events.CALENDAR_ID, 1); 
     values.put(CalendarContract.Events.EVENT_TIMEZONE, 
     Calendar.getInstance().getTimeZone().getID()); 
     Uri uri=cr.insert(CalendarContract.Events.CONTENT_URI, 
     values); 
     long eventId = Long.parseLong(uri.getLastPathSegment()); 

对于Android M及以上版本的设备,CALENDAR_ID设置为3,对于版本低于M的设置,值为1适用于大多数设备。

但它造成了少数devcives的问题。就像目前我遇到的问题,使用android verion 6.0的Lenove设备,其值为1,以便创建适当的事件。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ 
    values.put(Events.CALENDAR_ID, 3); 
}else{ 
    values.put(Events.CALENDAR_ID, 1); 
    } 
+0

请参阅https://stackoverflow.com/questions/35776265/android-create-calendar-event-always-as-birthday – natashadawra