I am having trouble in finding a solution to my problem. I am trying to add multiple events into my Android calendar but I do not know how. I gave found this code :
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", cal.getTimeInMillis());
intent.putExtra("allDay", true);
intent.putExtra("rrule", "FREQ=YEARLY");
intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
intent.putExtra("title", "A Test Event from android app"+String.valueOf(i));
startActivity(intent);
but all it does is to send me to my calendar and lets me edit and manually insert my event. I want to insert the events automatically, without going to my calendar app. Moreover, I can add only one event using this code. This is not what I wanted. I have 2 String arrays, one containing the date, and one containing the name of the event. Does anyone know if there is a way to do this? I have been searching for a solution for quite a while and hadn't found one. I would be grateful if anyone helped me. Thank you!
I have solved the problem! Thank you very much! But now I have another issue that I can't solve. I have this code :
ContentValues cv = new ContentValues();
cv.put("calendar_id", calIds[0]);
cv.put("title", title);
//cv.put("dtstart", dtstart );
//cv.put("dtend", dtend);
cv.put("rrule", "FREQ=MONTHLY");
cv.put("description", comment );
Calendar start = Calendar.getInstance();
start.set(2012, 0, 2, 8, 0, 0);
Calendar end = Calendar.getInstance();
end.set(2012, 0, 2, 9, 0, 0);
long startTime = start.getTimeInMillis();
long endTime = end.getTimeInMillis();
cv.put("dtstart", startTime);
cv.put("dtend", endTime);
//Insertion on the events of the calendar
cr.insert(Uri.parse("content://com.android.calendar/events"), cv);
It inserts my event but it doesn't do it recurring. I mean that my event appears on 2jan2012 but on 2jan2013 doesn't and neither does in 2014 and so on. So I opened my calendar on the phone and tried to edit my event and I saw that where I should select the occurence of it, there it shows on 2 january 2012 not on 2 january as it should if it were to customize my event from my phone. On the other hand, if I try to add an event manually from my phone it works just fine( I mean I can add an event that occurs yearly).
android SDK does not expose any interfaces to manipulate calendar, but you can find that from android source code. Android stores calendar in an internal sqlite database, though it's protected from direct access but you can still add/delete/update/query calendar data through ContentResolver. A piece of code to insert an event could be like this:
public String addEvent(String calendarId, String title, long startTime,
long endTime, int allDay) {
ContentValues event = new ContentValues();
event.put("calendar_id", calendarId); // "" for insert
event.put("title", title);
event.put("description", "");
event.put("eventLocation", "");
event.put("allDay", allDay);
event.put("eventStatus", 1);
event.put("transparency", 0);
event.put("dtstart", startTime);
event.put("dtend", endTime);
ContentResolver contentResolver = this.context.getContentResolver();
Uri eventsUri = Uri.parse("content://com.android.calendar/calendars");
Uri url = contentResolver.insert(eventsUri, event);
String ret = url.toString();
return ret;
}
When you insert one event successfully, a string represent uri of the event is returned by ContentResolver.insert, you can query, update or delete it later. In earlier SDKs before SDK 8, the content uri of calendar is "content://calendar/calendars" which differs from SDks 8 and after.
Meanwhile, be careful with those custom roms. since calendar API is not noted in SDK docs, the calendar provider could be modified or even removed by some vendors and operators, so you may have to test your application against lots of devices.
Good luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With