事件添加Android设备日历应用4.0

时间:2013-10-30 05:34:24

标签: android calendar

我想在Android设备日历App 4.0中添加Event。如果我在Calendar API中使用ID = 1,则Event成功添加,但如果我使用不同的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);

1 个答案:

答案 0 :(得分: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());
    }
    }