Inset Calender event and reminder - android

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 + "");
}

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

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

How to create a reminder notification in a specific day of the year?

I am trying to notify my app users with a notification in a specific day in the year and I am stuck in the middle as I did scheduled notifications at a regular bases (daily, weekly,..) and I don't want that.
Here is the 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", false);
intent.putExtra("rrule", "FREQ=DAILY");
intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
intent.putExtra("title", "A Test Event from android app");
startActivity(intent);
private String getCalendarUriBase(Activity act) {
String calendarUriBase = null;
Uri calendars = Uri.parse("content://calendar/calendars");
Cursor managedCursor = null;
try {
managedCursor = act.managedQuery(calendars, null, null, null, null);
} catch (Exception e) {
}
if (managedCursor != null) {
calendarUriBase = "content://calendar/";
} else {
calendars = Uri.parse("content://com.android.calendar/calendars");
try {
managedCursor = act.managedQuery(calendars, null, null, null, null);
} catch (Exception e) {
}
if (managedCursor != null) {
calendarUriBase = "content://com.android.calendar/";
}
}
return calendarUriBase; }
// get calendar
Calendar cal = Calendar.getInstance();
Uri EVENTS_URI = Uri.parse(getCalendarUriBase(this) + "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("dtstart", cal.getTimeInMillis() + 11*60*1000); // event starts at 11 minutes from now
values.put("dtend", cal.getTimeInMillis()+60*60*1000); // ends 60 minutes from now
values.put("description", "Reminder description");
values.put("visibility", 0);
values.put("hasAlarm", 1);
Uri event = cr.insert(EVENTS_URI, values);
// reminder insert
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 );
manifest
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
But I want to create a reminder notification in a specific day of the year not at regular bases (daily, weekly, etc). How can I write the code for that?
Use AlarmManager. Something like this:
Calendar cal = Calendar.getInstance();
cal.setTime(...);
Intent i = new Intent(context, YourBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);
You can use alarmManager service for this purpose!

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