1

我想在 Android 设备日历 App 4.0 中添加事件。如果我在日历 API 中使用 ID=1,则事件成功添加,但如果我使用不同的 ID,则事件不会添加到日历应用程序中。任何建议都是可取的。在此先感谢我使用以下代码

long ids = -1;
long startMillis = 0; 
long endMillis = 0; 
Calendar beginTime = Calendar.getInstance(); 
//beginTime.set(2013, 10, 29, 5,40); 
startMillis = beginTime.getTimeInMillis(); 
 Calendar endTime = Calendar.getInstance(); 
// endTime.set(2013, 10, 30, 4, 20);
 endMillis= endTime.getTimeInMillis();
Calendar cal = Calendar.getInstance();
// Insert Event
cr=getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.CALENDAR_ID, eventId);
values.put(CalendarContract.Events.TITLE, title);
values.put(CalendarContract.Events.DTSTART, startMillis);
values.put(CalendarContract.Events.DTEND, endMillis+60*60*1000);
//values.put(CalendarContract.Events.STATUS, "null");
TimeZone tz = TimeZone.getDefault();
values.put(CalendarContract.Events.EVENT_TIMEZONE, tz.getID());

Uri uri = cr.insert(
        CalendarContract.Events.CONTENT_URI, values);
4

1 回答 1

0

确保您要插入数据的日历 ID 存在:

检查一次:

public void getCalendarsList() {
    String[] projection = new String[]{"_id"};
    Uri calendars;
    if (Build.VERSION.SDK_INT >= 8 ) {
        calendars = Uri.parse("content://com.android.calendar/calendars");
    } else {
        calendars = Uri.parse("content://calendar/calendars");
    }
    Cursor cursor = this.managedQuery(calendars, projection, null, null, null);    //get all calendars
    if (cursor.moveToFirst()) {
        String calId;
        int l_idCol = cursor.getColumnIndex(projection[0]);
        do {
            calId = cursor.getString(l_idCol);

        } while (cursor.moveToNext());
    }
    }
于 2013-10-30T05:48:55.500 回答