java - How to show added events in Android default Calendar -
this code have used add calendar
events, it's working fine. can't see events have added. code doesn't have errors.
calendarview cal = (calendarview) rootview.findviewbyid(r.id.calendarview1); //context ctx; contentresolver cr = getactivity().getcontentresolver(); contentvalues event = new contentvalues(); event.put("calendar_id", r.id.calendarview1); event.put("title", "woot"); event.put("description", "wootification"); event.put("eventlocation", "wootness"); event.put("allday", 0); event.put("eventstatus", 1); //event.put("visibility", 0); //event.put("transparency", 0); // event.put(events.visible, 1); event.put("hasalarm", 0); calendar start = calendar.getinstance(); start.set(2015, 4, 27, 8, 0, 0); calendar end = calendar.getinstance(); end.set(2015, 4, 30, 9, 0, 0); long starttime = start.gettimeinmillis(); // starttime = starttime * 1000; long endtime = end.gettimeinmillis(); // endtime = endtime * 1000; timezone timezone = timezone.getdefault(); event.put(calendarcontract.events.event_timezone, timezone.getid()); event.put("dtstart", starttime); event.put("dtend", endtime); system.out.println(starttime); system.out.println(endtime); // insert event calendar uri eventsuri = uri.parse(calendarcontract.events.content_uri.tostring()); cr.insert(eventsuri, event); system.out.println(event); // add 10 minute reminder event contentvalues reminders = new contentvalues(); reminders.put(reminders.event_id, r.id.calendarview1); reminders.put(reminders.method, reminders.method_alert); reminders.put(reminders.minutes, 1); uri eventsurir = uri.parse("content://com.android.calendar/reminders"); cr.insert(eventsurir, reminders);
can me fix problem?
just in case if have not got solution.
use following method add, remove , delete event default calendar.
public void addevent(context context) { gregoriancalendar caldate = new gregoriancalendar(this._year, this._month, this._day, this._hour, this._minute); try { contentresolver cr = context.getcontentresolver(); contentvalues values = new contentvalues(); values.put(calendarcontract.events.dtstart, caldate.gettimeinmillis()); values.put(calendarcontract.events.dtend, caldate.gettimeinmillis()+60*60*1000); values.put(calendarcontract.events.title, this._title); values.put(calendarcontract.events.calendar_id, 1); values.put(calendarcontract.events.event_timezone, calendar.getinstance() .gettimezone().getid()); system.out.println(calendar.getinstance().gettimezone().getid()); uri uri = cr.insert(calendarcontract.events.content_uri, values); // save eventid task object possible future delete. this._eventid = long.parselong(uri.getlastpathsegment()); // add 5 minute, 1 hour (2 reminders) setreminder(cr, this._eventid, 5); setreminder(cr, this._eventid, 60); } catch (exception e) { e.printstacktrace(); } } // routine add reminders event public void setreminder(contentresolver cr, long eventid, int timebefore) { try { contentvalues values = new contentvalues(); values.put(calendarcontract.reminders.minutes, timebefore); values.put(calendarcontract.reminders.event_id, eventid); values.put(calendarcontract.reminders.method, calendarcontract.reminders.method_alert); uri uri = cr.insert(calendarcontract.reminders.content_uri, values); cursor c = calendarcontract.reminders.query(cr, eventid, new string[]{calendarcontract.reminders.minutes}); if (c.movetofirst()) { system.out.println("calendar" + c.getint(c.getcolumnindex(calendarcontract.reminders.minutes))); } c.close(); } catch (exception e) { e.printstacktrace(); } } //use following method remove event calendar using eventid stored within task object. public void removeevent(context context) { contentresolver cr = context.getcontentresolver(); int inumrowsdeleted = 0; uri eventsuri = uri.parse(calendar_uri_base+"events"); uri eventuri = contenturis.withappendedid(eventsuri, this._eventid); inumrowsdeleted = cr.delete(eventuri, null, null); log.i(debug_tag, "deleted " + inumrowsdeleted + " calendar entry."); } public int updateevent(context context) { int inumrowsupdated = 0; gregoriancalendar caldate = new gregoriancalendar(this._year, this._month, this._day, this._hour, this._minute); contentvalues event = new contentvalues(); event.put(calendarcontract.events.title, this._title); event.put("hasalarm", 1); // 0 false, 1 true event.put(calendarcontract.events.dtstart, caldate.gettimeinmillis()); event.put(calendarcontract.events.dtend, caldate.gettimeinmillis()+60*60*1000); uri eventsuri = uri.parse(calendar_uri_base+"events"); uri eventuri = contenturis.withappendedid(eventsuri, this._eventid); inumrowsupdated = context.getcontentresolver().update(eventuri, event, null, null); // todo put text strings.xml log.i(debug_tag, "updated " + inumrowsupdated + " calendar entry."); return inumrowsupdated; }
Comments
Post a Comment