How to add schedule to calendar #Android - 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;
}

Related

Inset Calender event and reminder

I want to add a reminder in google calender's through my android application(android studio) but I have tried everything but it doesn't add in the calendar. I have also tried to add through the https://developer.android.com/guide/topics/providers/calendar-provider#events
but still no success.
Date d1 = new Date(1472570400);//Tue, 30 Aug 2016 15:20:00 GMT
Calendar cal1 = Calendar.getInstance();
cal1.setTime(d1);
Date d2 = new Date(1472574000);//Tue, 30 Aug 2016 16:20:00 GMT
Calendar cal2 = Calendar.getInstance();
cal2.setTime(d2);
Uri EVENTS_URI = Uri.parse("content://com.android.calendar/events");
ContentResolver cr = getContentResolver();
// event insert
ContentValues values = new ContentValues();
values.put("calendar_id", 1);
values.put("title", "Reminder Title");
values.put("allDay", 0);
values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
values.put("dtstart", cal1.getTimeInMillis() ); // event starts at Tue, 30 Aug 2016 15:20:00
values.put("dtend", cal2.getTimeInMillis()); // ends at Tue, 30 Aug 2016 16:20:00 GMT
System.out.println("ALARM TIMES START : " + cal1.getTimeInMillis());
System.out.println("ALARM TIMES END : "+cal2.getTimeInMillis());
values.put("description", "Reminder description");
values.put("hasAlarm", 1);
Uri event = cr.insert(EVENTS_URI, values);
// reminder insert
Uri REMINDERS_URI = Uri.parse("content://com.android.calendar/reminders");
values = new ContentValues();
values.put( "event_id", Long.parseLong(event.getLastPathSegment()));
values.put( "method", 1);
values.put( "minutes", 1); //Notify before the exact time
cr.insert( REMINDERS_URI, values );
public void insertCalendarDetail(String cal_id,String cal_date,String title,String desc,String cat_id)//,String cat_name
{
Uri event_id=null;
String[] cal_dates = cal_date.split("-");
Log.e("cal_dates==>"+cal_id,cal_dates[0]+"/"+cal_dates[1]+"/"+cal_dates[2]);
java.util.Calendar startTime = java.util.Calendar.getInstance();
startTime.set(Integer.parseInt(cal_dates[2]),Integer.parseInt(cal_dates[1])-1,Integer.parseInt(cal_dates[0]), 3, 00);
long startMillis = startTime.getTimeInMillis();
long endMillis = startTime.getTimeInMillis()+60 * 60 * 1000;;
final ContentValues event = new ContentValues();
event.put(CalendarContract.Events.CALENDAR_ID,1);
event.put(CalendarContract.Events.TITLE,title);
event.put(CalendarContract.Events.DESCRIPTION, Html.fromHtml(desc)+"");
event.put(CalendarContract.Events.DTSTART, startMillis);
event.put(CalendarContract.Events.DTEND, endMillis);
String timeZone = TimeZone.getDefault().getID();
event.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone);
Uri baseUri;
if (Build.VERSION.SDK_INT >= 8)
{
baseUri = Uri.parse("content://com.android.calendar/events");
}
else
{
baseUri = Uri.parse("content://calendar/events");
}
event_id = context.getContentResolver().insert(baseUri, event);
Log.e("calendar_insert", event_id + "");
}

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());
}
}

Android create calendar event

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.

Categories

Resources