简体   繁体   中英

Adding Event in Android Calendar

private void setEvent(){        
    long startMilis = 0;
    int mCalId = 1;
    long endMilis = 0;
    Calendar beginTime = Calendar.getInstance();
    beginTime.set(2013, 1, 29, 9, 10);
    startMilis = beginTime.getTimeInMillis();  
    Calendar endTime = Calendar.getInstance();
    endTime.set(2013, 1, 30, 10,10);
    endMilis = endTime.getTimeInMillis();

    ContentResolver cr = getContentResolver();
    ContentValues values = new ContentValues();
    values.put(Events.CALENDAR_ID, mCalId);
    values.put(Events.DTSTART, startMilis);
    values.put(Events.DTEND, endMilis);
    values.put(Events.TITLE,"Special Event");
    values.put(Events.DESCRIPTION, "Group Activity");
    values.put(Events.EVENT_TIMEZONE, "America/Los_Angeles");
    Uri uri = cr.insert(Events.CONTENT_URI, values);
    Toast.makeText(this, "Event Added", Toast.LENGTH_LONG).show();
}

This is snippet of my code where i want to add Event to the android calendar. Code works fine.

But when tested on device, The specified event does not actually get added and does not appear in calendar .Code is totally Error free and i have provided necessary permissions.

Can someone please tell me where exactly i am doing wrong.

If there are no errors, but nevertheless the event does not appear in the Calendar, I would suspect that the culprit is the mCalId = 1 assignment.

A device can have multiple calendars. It's not guaranteed that the one with id == 1 is the primary one (and even if you used the primary one, the user may have events in multiple different calendars -- eg personal and work).

So, it depends on what you want to do exactly. You should either:

  • use the primary calendar (the one with IS_PRIMARY , but see caveat above), or
  • have the user select a calendar first (only if more than one is found when querying the Calendars table ) and then using that calendar_id from then on.

Maybe you are usin wrong Uri, i use this in my app:

    Uri EVENTS_URI = Uri.parse(CalendarContract.Events.CONTENT_URI.toString());
    ContentResolver cr = getActivity().getContentResolver();

    ContentValues values = new ContentValues();

    values.put("calendar_id", 1);       
    values.put(Events.TITLE, recordatorio);
    values.put(Events.ALL_DAY, 1);
    values.put(Events.EVENT_LOCATION, lugar);
    values.put("dtstart", calDate.getTimeInMillis());
    values.put("dtend", calDate.getTimeInMillis());
    values.put(Events.DESCRIPTION, observaciones);
    values.put("availability", 0);
    values.put(Events.HAS_ALARM, true);     
    values.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().toString());

    Uri uri = cr.insert(EVENTS_URI, values);
// to get the Id Event          
    long eventID = Long.parseLong(uri.getLastPathSegment());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM