Android create calendar event - android

I need to create multiple calendar event for Android application, Using this question I was able to create single event.
Is there any example or guide for create multiple calender events?
Thank You,
Chandana

place these in a function
like
public void calenderevent(Calendar begintime, Calendar endtime){
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", begintime.getTimeInMillis());
intent.putExtra("allDay", true);
intent.putExtra("rrule", "FREQ=YEARLY");
intent.putExtra("endTime", endtime.getTimeInMillis()+60*60*1000);
intent.putExtra("title", "A Test Event from android app");
startActivity(intent);
}

As of ICS there is a better answer, as documented in the blog there is now an official API.
Blog entry on the calendar APIs in ICS
Here's the documentation on developer.android.com
Cheers!

This is all about above Android Build API 8 to ICS 15.
String[] calendarsProjection = {
CalendarContract.Calendars._ID,
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
CalendarContract.Calendars.ACCOUNT_NAME
};
String calName;
String calId = null;
Uri calendars= Uri.parse("content://com.android.calendar/events");
Cursor managedCursor = managedQuery(calendars, calendarsProjection, null, null, null);
if (managedCursor.moveToFirst())
{
int nameColumn = managedCursor.getColumnIndex("account_name");
int idColumn = managedCursor.getColumnIndex("_id");
do
{
calName = managedCursor.getString(nameColumn);
calId = managedCursor.getString(idColumn);
Log.e("Calendar Id : ",""+calId+" : "+calName);
}
while (managedCursor.moveToNext());
}
long startMillis = 0;
long endMillis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2012, 6, 18, 13, 10, 10);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2012, 6, 18, 16, 10, 10);
endMillis = endTime.getTimeInMillis();
System.out.println("Date start :"+startMillis);
System.out.println("Date start :"+endMillis);
// Insert Event
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, startMillis);
values.put(CalendarContract.Events.DTEND, endMillis);
values.put(CalendarContract.Events.TITLE, "Walk The Dog");
values.put(CalendarContract.Events.DESCRIPTION, "My dog is bored, so we're going on a really long walk!");
values.put(CalendarContract.Events.CALENDAR_ID, 1 );
values.put(CalendarContract.Events.EVENT_TIMEZONE, "UTC");
Uri uri = cr.insert(Uri.parse("content://com.android.calendar/events"), values);

String[] calendarsProjection = {
CalendarContract.Calendars._ID,
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
CalendarContract.Calendars.ACCOUNT_NAME
};
String calName;
String calId = null;
Uri calendars= Uri.parse("content://com.android.calendar/events");
Cursor managedCursor = managedQuery(calendars, calendarsProjection, null, null, null);
if (managedCursor.moveToFirst())
{
int nameColumn = managedCursor.getColumnIndex("account_name");
int idColumn = managedCursor.getColumnIndex("_id");
do
{
calName = managedCursor.getString(nameColumn);
calId = managedCursor.getString(idColumn);
Log.e("Calendar Id : ",""+calId+" : "+calName);
}
while (managedCursor.moveToNext());
}
long startMillis = 0;
long endMillis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2012, 6, 18, 13, 10, 10);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2012, 6, 18, 16, 10, 10);
endMillis = endTime.getTimeInMillis();
System.out.println("Date start :"+startMillis);
System.out.println("Date start :"+endMillis);
// Insert Event
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, startMillis);
values.put(CalendarContract.Events.DTEND, endMillis);
values.put(CalendarContract.Events.TITLE, "Walk The Dog");
values.put(CalendarContract.Events.DESCRIPTION, "My dog is bored, so we're going on a really long walk!");
values.put(CalendarContract.Events.CALENDAR_ID, 1 );
values.put(CalendarContract.Events.EVENT_TIMEZONE, "UTC");
Uri uri = cr.insert(Uri.parse("content://com.android.calendar/events"), values);

Well..the last 2 post works fine in ICS but not in others. I suggest this class from google code.

Related

How to add schedule to calendar #Android

I want to create new schedule programmatically.I parsed title,
place and description as parameter.Date and time are calculated as fix value.Here is my code to add schedule to calendar..
public Uri addScheduleToCalender(String title,String place,String description) {
long calID = 3;
long startMillis = 0;
long endMillis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2016, 7, 22, 17, 30);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2016, 7, 22, 18, 45);
endMillis = endTime.getTimeInMillis();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.EVENT_LOCATION, place);
values.put(CalendarContract.Events.DTSTART, startMillis);
values.put(CalendarContract.Events.DTEND, endMillis);
values.put(CalendarContract.Events.TITLE, title);
values.put(CalendarContract.Events.DESCRIPTION, description);
values.put(CalendarContract.Events.CALENDAR_ID, calID);
values.put(CalendarContract.Events.EVENT_TIMEZONE, "UTC/GMT +2:00");
Uri uri;
if (Build.VERSION.SDK_INT >= 8) {
uri = Uri.parse("content://com.android.calendar/events");
} else {
uri = Uri.parse("content://calendar/events");
}
Uri l_uri = MainActivity.this.getContentResolver()
.insert(uri, values);
return l_uri;
}
but it doesn't work.it doesn't save any schedule data.
For inserting schedule task in default calendar you should use below code ,
// You need to pass application context as ctx parameter
ContentResolver cr = ctx.getContentResolver();
// Now add schedule as below
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
Try this code:
public Uri addScheduleToCalender(String title,String place,String description) {
long calID = 3;
long startMillis = 0;
long endMillis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2016, 07, 22, 17, 30);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2016, 07, 22, 18, 45);
endMillis = endTime.getTimeInMillis();
ContentValues values = new ContentValues();
ContentResolver cr = getContentResolver();
values.put(CalendarContract.Events.EVENT_LOCATION, place);
values.put(CalendarContract.Events.DTSTART, startMillis);
values.put(CalendarContract.Events.DTEND, endMillis);
values.put(CalendarContract.Events.TITLE, title);
values.put(CalendarContract.Events.DESCRIPTION, description);
values.put(CalendarContract.Events.CALENDAR_ID, calID);
values.put(CalendarContract.Events.EVENT_TIMEZONE, "UTC/GMT +2:00");
Uri uri;
if (Build.VERSION.SDK_INT >= 8) {
uri = Uri.parse("content://com.android.calendar/events");
} else {
uri = Uri.parse("content://calendar/events");
}
Uri l_uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
return l_uri;
}

Add an event to calendar

I'm using the following code (from developer site) for inserting an event in device calendar-
long calID = 1;
long startMillis = 0;
long endMillis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2015, 9, 6, 7, 00);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2015, 9, 6, 8, 45);
endMillis = endTime.getTimeInMillis();
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(Events.DTSTART, startMillis);
values.put(Events.DTEND, endMillis);
values.put(Events.TITLE, "Test Title");
values.put(Events.DESCRIPTION, "Group workout");
values.put(Events.CALENDAR_ID, calID);
TimeZone tz = TimeZone.getDefault();
values.put(Events.EVENT_TIMEZONE, tz.getID());
Uri uri = cr.insert(Events.CONTENT_URI, values);
long eventID = Long.parseLong(uri.getLastPathSegment());
The code seems to work because I'm getting eventID of the inserted event.
After executing this, I'm not able to see the event in the calendar app. I tried with syncing the calendar from settings>accounts>google>calendar.
Is there something wrong in the code or any additional code that I've to add to see the events in the calendar?
Thanks to #Nobu Games, I solved the issue. The problem was calendarId = 1 was not associated with any of the calendar. I created a method to find out available calendarIds in the device using following code-
public MyCalendar [] getCalendar(Context c) {
String projection[] = {"_id", "calendar_displayName"};
Uri calendars;
calendars = Uri.parse("content://com.android.calendar/calendars");
ContentResolver contentResolver = c.getContentResolver();
Cursor managedCursor = contentResolver.query(calendars, projection, null, null, null);
if (managedCursor.moveToFirst()){
m_calendars = new MyCalendar[managedCursor.getCount()];
String calName;
String calID;
int cont= 0;
int nameCol = managedCursor.getColumnIndex(projection[1]);
int idCol = managedCursor.getColumnIndex(projection[0]);
do {
calName = managedCursor.getString(nameCol);
calID = managedCursor.getString(idCol);
m_calendars[cont] = new MyCalendar(calName, calID);
cont++;
} while(managedCursor.moveToNext());
managedCursor.close();
}
return m_calendars;
}
And then I'm able to see the events in the calendar.

Consultation abount Reminder to calendar

i trying to make reminder for my calendar event.
this is my code for the event.
for (long gg=1;gg<=4;gg++)
{
long calID = gg;
long startMillis = 0;
long endMillis = 0;
Calendar cal = Calendar.getInstance();
MyDay = cal.get(Calendar.DAY_OF_MONTH);
MyMonth = cal.get(Calendar.MONTH);
MyYear = cal.get(Calendar.YEAR);
MyHoure = cal.get(Calendar.HOUR_OF_DAY);
MyMinute =cal.get(Calendar.MINUTE);
Calendar beginTime = Calendar.getInstance();
beginTime.set(MyYear, MyMonth, MyDay , MyHoure+1, MyMinute);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(MyYear, MyMonth, MyDay , MyHoure+1, MyMinute);
endMillis = endTime.getTimeInMillis();
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(Events.DTSTART, startMillis);
values.put(Events.DTEND, endMillis);
values.put(Events.TITLE, "Test Title");
values.put(Events.DESCRIPTION, "My Text ....." );
values.put(Events.CALENDAR_ID, calID);
values.put(Events.EVENT_TIMEZONE, "Israel/tel-aviv");
try{
Uri uri = cr.insert(Events.CONTENT_URI, values);
}
catch (Exception ex)
{
}
}
what i need to add to my code for making reminder for 10 minute before the event start ?
i try something like this:
Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(this) + "reminders");
values = new ContentValues();
values.put( "event_id", Long.parseLong(event.getLastPathSegment()));
values.put( "method", 1 );
values.put( "minutes", 10 );
cr.insert( REMINDERS_URI, values );
But it does not work.
thanks
I've gone through your code and I believe that the piece missing in this puzzle is putting the hasAlarm value in your content values which is off by default...
values.put(Events.HAS_ALARM, true);

Event Add Android device Calendar App 4.0

I want to add Event in Android device calendar App 4.0 .If I use ID=1 in Calendar API then Event successfully add but if i use different id then event does not add in calendar app. Any suggesstion would be appriciable. Thanks in advance I use Following code
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);
Make Sure the Calendar ID which you are inserting data exists :
check once :
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());
}
}

How to add reminder in calendar

Hi i am implementing birthday reminder app on android. please tell me what is the procedure to set reminder on calendar. I have obtained the calendar in android .
You can use this for adding events in the Android Calendar.
GregorianCalendar startDate = new GregorianCalendar(Locale.ENGLISH);
startDate = CustomDateFormatter.formatScheduleDate(schedule.getScheduleStartDate());
GregorianCalendar endDate = new GregorianCalendar(Locale.ENGLISH);
endDate = CustomDateFormatter.formatScheduleDate(schedule.getScheduleEndDate());
try{
String[] projection = new String[] { "_id", "name" };
Uri calendars = Uri.parse("content://com.android.calendar/calendars");
Cursor managedCursor = mContext.getContentResolver().query(calendars, projection, "selected=1", null, null);
ContentValues event = new ContentValues();
long StartTime = startDate.getTimeInMillis();
long EndTime = endDate.getTimeInMillis();
// int nameColumn = managedCursor.getColumnIndex("name");
int idColumn = managedCursor.getColumnIndex("_id");
if(managedCursor.moveToFirst()){
// String calName = managedCursor.getString(nameColumn);
String calId = managedCursor.getString(idColumn);
// Log.e("Cal name", calName);
event.put("calendar_id", calId);
event.put("title", schedule.getScheduleType());
event.put("description", schedule.getScheduleTextContent());
event.put("dtstart", StartTime );
event.put("dtend", EndTime);
event.put("hasAlarm", 1);
Uri eventsUri = Uri.parse("content://com.android.calendar/events");
Uri calUri = mContext.getContentResolver().insert(eventsUri, event);
Uri remindersUri = Uri.parse("content://com.android.calendar/reminders");
event = new ContentValues();
event.put("event_id", Long.parseLong(calUri.getLastPathSegment()));
// Log.d("Event ID: ", calUri.getLastPathSegment());
event.put("method",1);
event.put("minutes",0);
mContext.getContentResolver().insert(remindersUri, event);
}
managedCursor.close();
}catch(Exception ex){
ex.printStackTrace();
}

Categories

Resources