How to programmatically populate Reminders section on Android device's Calendar app? - android

I need to open Android device's Calendar app with some pre-populated data. The logic that Iam using seems to populate fields like:
Event description
Event location
From Date, To Date
All day event/not
Repeat /Recurrence Info
I am not able to populate "Reminders" section, I would like to populate Reminders section. It will great to get some help on this
Here is the code that I am using to open Calendar app and populate date.
// Intent to open Calendar Event
Intent intent = new Intent(Intent.ACTION_INSERT)
.setData(Events.CONTENT_URI);
intent.putExtra(Events.DESCRIPTION, desc);
intent.putExtra(Events.EVENT_LOCATION, location);
intent.putExtra(Events.TITLE, summary);
intent.putExtra(Events.EVENT_TIMEZONE, beginTime.getTimeZone().getID());
intent.putExtra(Events.STATUS, statusStr);
intent.putExtra(Events.VISIBLE, transparency);
intent.putExtra(Events.RRULE, "FREQ=YEARLY;INTERVAL=1;BYYEARDAY=1,2;UNTIL=20161210;");
intent.putExtra(Events.EXDATE, androidExDateStr.toString());
// Not sure on how to use CalendarContract.Reminders, Tried the following but does not seem to be working
intent.putExtra(CalendarContract.Reminders.DESCRIPTION, desc);
intent.putExtra(CalendarContract.Reminders.EVENT_LOCATION, location);
intent.putExtra(CalendarContract.Reminders.TITLE, summary);
intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis());
intent.putExtra(CalendarContract.Reminders.DTSTART, beginTime.getTimeInMillis());
intent.putExtra(CalendarContract.Reminders.EVENT_TIMEZONE, beginTime.getTimeZone().getID());
intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis());
intent.putExtra(CalendarContract.Reminders.DTEND, endTime.getTimeInMillis());
intent.putExtra(CalendarContract.Reminders.STATUS, statusStr);
intent.putExtra(CalendarContract.Reminders.RRULE,"FREQ=YEARLY;INTERVAL=1;BYYEARDAY=1,2;UNTIL=20161210;");
intent.putExtra(CalendarContract.Reminders.EXDATE, androidExDateStr.toString());
//intent.putExtra(CalendarContract.Reminders.METHOD, Reminders.METHOD_EMAIL);
//intent.putExtra(CalendarContract.Reminders.MINUTES, reminderVal) ;
//intent.putExtra(CalendarContract.Events.HAS_ALARM, 1);
//}
try {
context.startActivity(intent);
} catch(Exception e) {
e.printStackTrace();
Log.v(LOG_TAG, "Cannot schedule Calendar event as specified ");
return false;
}

Did you check the example from http://developer.android.com/guide/topics/providers/calendar-provider.html#reminders?
long eventID = 221;
...
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(Reminders.MINUTES, 15);
values.put(Reminders.EVENT_ID, eventID);
values.put(Reminders.METHOD, Reminders.METHOD_ALERT);
Uri uri = cr.insert(Reminders.CONTENT_URI, values);

Related

CalendarContract not setting calendar name properly

I'm creating a calendar programatically, and everything is setted well but the calendar's name. This is the method I'm using.
private Uri createCalendar(String email){
Uri target = Uri.parse(CalendarContract.Calendars.CONTENT_URI.toString());
target = target.buildUpon().appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, email)
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, "com.google").build();
ContentValues values = new ContentValues();
values.put(CalendarContract.Calendars.ACCOUNT_NAME, email);
values.put(CalendarContract.Calendars.ACCOUNT_TYPE, "com.google");
values.put(CalendarContract.Calendars.NAME, context.getResources().getString(R.string.app_name));
values.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, context.getResources().getString(R.string.app_name));
values.put(CalendarContract.Calendars.CALENDAR_COLOR, Util.getColor(context, R.color.primary));
values.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_ROOT);
values.put(CalendarContract.Calendars.OWNER_ACCOUNT, email);
values.put(CalendarContract.Calendars.VISIBLE, 1);
values.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
values.put(CalendarContract.Calendars.CALENDAR_TIME_ZONE, TimeZone.getDefault().getID());
values.put(CalendarContract.Calendars.CAN_PARTIALLY_UPDATE, 1);
values.put(CalendarContract.Calendars.CAL_SYNC1, "https://www.google.com/calendar/feeds/" + email + "/private/full");
values.put(CalendarContract.Calendars.CAL_SYNC2, "https://www.google.com/calendar/feeds/default/allcalendars/full/" + email);
values.put(CalendarContract.Calendars.CAL_SYNC3, "https://www.google.com/calendar/feeds/default/allcalendars/full/" + email);
values.put(CalendarContract.Calendars.CAL_SYNC4, 1);
values.put(CalendarContract.Calendars.CAL_SYNC5, 0);
values.put(CalendarContract.Calendars.CAL_SYNC8, System.currentTimeMillis());
Uri newCalendar = context.getContentResolver().insert(target, values);
return newCalendar;
}
As said, everything is working fine, the color, the owner account, etc. but when the calendar is created, the name of it is always "Events". I tried to put the name manually (not using getStrings()) but didn't work either.
Any ideas of what can be happening?

Issue in Adding Calendar Event in Android M

I am using a code to add an event to the device's calendar. The code works fine in all the Android versions except Android M.The event when being added on Android M shows as a birthday instead of an event. Please help on how can this issue be fixed.
The code being used is as follows
// Add event to calendar
try {
ContentResolver cr = context.getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, startDateInMilliSeconds);
values.put(CalendarContract.Events.DTEND, endDateInMilliSeconds);
values.put(CalendarContract.Events.TITLE, title);
values.put(CalendarContract.Events.DESCRIPTION, description);
if (location != null && (!TextUtils.isEmpty(location))) {
values.put(CalendarContract.Events.EVENT_LOCATION, location);
}
values.put(CalendarContract.Events.CALENDAR_ID, 1);
values.put(CalendarContract.Events.EVENT_TIMEZONE, Calendar.getInstance()
.getTimeZone().getID());
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
// Save the eventId into the Task object for possible future delete.
long eventId = Long.parseLong(uri.getLastPathSegment());
Uri openCalendarEvent = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventId);
Intent calIntent = new Intent(Intent.ACTION_VIEW).setData(uri);
calIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, startDateInMilliSeconds);
calIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endDateInMilliSeconds);
calIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(calIntent);
} catch (Exception e) {
e.printStackTrace();
}
try below method # Himmat
public void addReminder() {
Intent calIntent = new Intent(Intent.ACTION_EDIT);
calIntent.setData(CalendarContract.Events.CONTENT_URI);
calIntent.setType("vnd.android.cursor.item/event");
calIntent.putExtra(CalendarContract.Events.TITLE, e.getName());
calIntent.putExtra(CalendarContract.Events.EVENT_LOCATION, e.getLocation());
calIntent.putExtra(CalendarContract.Events.DESCRIPTION, e.getDescription());
Calendar calStart=Calendar.getInstance();
if(Validator.isNotNull(e.getStartDate())) {
calStart.setTime(e.getStartDate());
}
calIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,
calStart.getTimeInMillis());
Calendar calEnd=Calendar.getInstance();
if(Validator.isNotNull(e.getEndDate())) {
calEnd.setTime(e.getEndDate());
}
calIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,
calEnd.getTimeInMillis());
calIntent.putExtra(CalendarContract.EXTRA_CUSTOM_APP_URI,
e.getRegistrationLink());
startActivity(calIntent);
}

Remove and Update existing Calendar Event

I am following this gist, to insert event into Calendar
How do I update existing Calendar Event, which i have inserted earlier using below code:
public void addToCalender() throws ParseException {
......
ContentValues event = new ContentValues();
event.put(CalendarContract.Events.CALENDAR_ID, calendarId[0]);
event.put(CalendarContract.Events.TITLE, "Event Title");
event.put(CalendarContract.Events.DESCRIPTION, "Event Description");
event.put(CalendarContract.Events.EVENT_LOCATION, "Eevnt Location");
event.put(CalendarContract.Events.DTSTART, startCalTime);
event.put(CalendarContract.Events.DTEND, endCalTime);
event.put(CalendarContract.Events.STATUS, 1);
event.put(CalendarContract.Events.HAS_ALARM, 1);
event.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
Uri insertEventUri = AddEventActivity.this.getContentResolver().insert(
eventsUri, event);
ContentValues reminders = new ContentValues();
reminders.put(Reminders.EVENT_ID,
Long.parseLong(insertEventUri.getLastPathSegment()));
reminders.put(Reminders.METHOD, Reminders.METHOD_ALERT);
reminders.put(Reminders.MINUTES, 10);
AddEventActivity.this.getContentResolver().insert(remainderUri, reminders);
}
I would like to know, How do I :
1. Remove existing event
2. Update existing event
Here is how you can modify an event. Let's say its ID is eventID:
public void updateEvent(int eventID)
{
ContentResolver cr = context.getContentResolver();
Uri eventUri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventID);
ContentValues event = new ContentValues();
event.put(CalendarContract.Events.TITLE, "new title");
event.put(CalendarContract.Events.DESCRIPTION, "My cool event!");
cr.update(eventUri, event, null, null);
}
To remove an event, you have two possibilities:
If you are a standard application:
Use the previous code and replace all the event.put by this one:
event.put(CalendarContract.Events.DELETED, 1);
If you are the SyncAdapter of the calendar and want a real deletion (The previous method just says that the event should be deleted and the one that follows must be used once the calendar is being synced :
public deleteEvent(Uri eventUri)
{
cr.delete(event, null, null);
}
Where eventUri is the Uri of the event obtained as shown previously from the event ID.
For all of the previous methods, if you get an exception about not being in a sync adapter, you can use this:
public static Uri asSyncAdapter(Uri uri, String account, String accountType)
{
return uri.buildUpon()
.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, account)
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, accountType)
.build();
}
I hope this will help you.
Source: personnal code + http://developer.android.com/guide/topics/providers/calendar-provider.html
...
Outlook in fact seems to respond to the rules defined in RFC 2446
In summary you have to specify
METHOD:REQUEST and ORGANIZER:xxxxxxxx
...
Please read the original answer by Tom Carter.

Android Add Event to Calendar using Intent, get EventID

I'm trying to add event through an calendar intent. However, I can't figure out how to get the event ID of the event just added.
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", sdate.getTime());
intent.putExtra("endTime", edate.getTime());
intent.putExtra("allDay", true);
intent.putExtra("rrule", "FREQ=YEARLY");
intent.putExtra("title", "A Test Event from android app");
intent.putExtra("description", "Description here");
intent.putExtra("eventLocation", "location here here here");
I read extensively on other resources and can't seem to find an answer. I tried startActivityForResult but I can't seem to get it work. Other methods I try can't seem to check for it until the activity ends.
Is there any other way to get the event ID after the event has been added to the calendar? I need to use the intent method for this.
Try this one solution:
import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
public class CalenderUtils {
/**
* Add a new event into a native Google calendar. Add alert notification by setting <code>isRemind</code> as <code>true</code>.
* #param cr - ContentResolver
* #param title - Event title
* #param addInfo - Event description
* #param place - Event place
* #param status - <code>int</code> This information is sufficient for most entries: tentative (0), confirmed (1) or canceled (2):
* #param startDate - <code>long</code> event start time in mls
* #param isRemind - <code>boolean</code> need to remind about event before?
* #param isMailService - <code>boolean</code>. Adding attendees to the meeting
* #return <code>long</code> eventID
*/
public static long addEventToCalender(ContentResolver cr, String title, String addInfo, String place, int status,
long startDate, boolean isRemind, boolean isMailService) {
String eventUriStr = "content://com.android.calendar/events";
ContentValues event = new ContentValues();
// id, We need to choose from our mobile for primary its 1
event.put("calendar_id", 1);
event.put("title", title);
event.put("description", addInfo);
event.put("eventLocation", place);
event.put("eventTimezone", "UTC/GMT +2:00");
// For next 1hr
long endDate = startDate + 1000 * 60 * 60;
event.put("dtstart", startDate);
event.put("dtend", endDate);
//If it is bithday alarm or such kind (which should remind me for whole day) 0 for false, 1 for true
// values.put("allDay", 1);
event.put("eventStatus", status);
event.put("hasAlarm", 1);
Uri eventUri = cr.insert(Uri.parse(eventUriStr), event);
long eventID = Long.parseLong(eventUri.getLastPathSegment());
if (isRemind) {
String reminderUriString = "content://com.android.calendar/reminders";
ContentValues reminderValues = new ContentValues();
reminderValues.put("event_id", eventID);
// Default value of the system. Minutes is a integer
reminderValues.put("minutes", 5);
// Alert Methods: Default(0), Alert(1), Email(2), SMS(3)
reminderValues.put("method", 1);
cr.insert(Uri.parse(reminderUriString), reminderValues); //Uri reminderUri =
}
if (isMailService) {
String attendeuesesUriString = "content://com.android.calendar/attendees";
/********* To add multiple attendees need to insert ContentValues multiple times ***********/
ContentValues attendeesValues = new ContentValues();
attendeesValues.put("event_id", eventID);
// Attendees name
attendeesValues.put("attendeeName", "xxxxx");
// Attendee email
attendeesValues.put("attendeeEmail", "yyyy#gmail.com");
// Relationship_Attendee(1), Relationship_None(0), Organizer(2), Performer(3), Speaker(4)
attendeesValues.put("attendeeRelationship", 0);
// None(0), Optional(1), Required(2), Resource(3)
attendeesValues.put("attendeeType", 0);
// None(0), Accepted(1), Decline(2), Invited(3), Tentative(4)
attendeesValues.put("attendeeStatus", 0);
cr.insert(Uri.parse(attendeuesesUriString), attendeesValues); //Uri attendeuesesUri =
}
return eventID;
}
}
i copypasted this class from my project so you are welcome to change names as you prefer.
As you notice here u receive id value after insertion.
If you need get reminderId or meeting id with attendees - you have to handle a return value from cr.insert... and then parce uri long id = Long.parseLong(uri.getLastPathSegment());
Here is an additional info: CalendarContract.Events and Calendar chapter

write in the android calendar

As part of a project, I created an android application which communicates with an online database (MySQL) to integrate the appointment has taken the online calendar.
I collect the data, converted to Json, but when the inscrires in the agenda of android mobile I encounter a probleme, here is my code : (sorry for my english )
EDIT :
public class calendrier extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Cursor cursor = getContentResolver()
.query(Uri.parse("content://com.android.calendar/calendars"),
new String[] { "_id", "displayName" }, "selected=1",
null, null);
if (cursor != null && cursor.moveToFirst()) {
String[] calNames = new String[cursor.getCount()];
int[] calIds = new int[cursor.getCount()];
for (int i = 0; i < calNames.length; i++) {
calIds[i] = cursor.getInt(0);
calNames[i] = cursor.getString(1);
cursor.moveToNext();
}
cursor.close();
if (calIds.length > 0) {
// we're safe here to do any further work
}
// grab calendar id from above
int cal_id = calIds[0];
// set the content value
ContentValues cv = new ContentValues();
cv.put("calendar_id", cal_id);
cv.put("title", "titre");
cv.put("description", "bla bla bla");
cv.put("eventLocation", "city");
// note: you're going to need to convert the desired date into
// milliseconds
cv.put("dtstart", System.currentTimeMillis());
cv.put("dtend", System.currentTimeMillis()
+ DateUtils.DAY_IN_MILLIS);
cv.put("allDay", 0); // true = 1, false = 0
cv.put("hasAlarm", 1);
// once desired fields are set, insert it into the table
getContentResolver().insert(
Uri.parse("content://com.android.calendar/events"), cv);
}
}
}
this code works but it asks me if I want to participate in the event when I open it and I wish he does not do
thanks
//did U include permission in your manifest.xml file
android.permission.READ_CALENDAR
android.permission.WRITE_CALENDAR
String WRITE_CALENDAR Allows an application to write (but not read) the user's calendar data.
The Uri of Calendar has changed from Android 2.2 .
Old (2.1 and before): content://calendar/
New (2.2): content://com.android.calendar/
Change your Uri to the new one.
There is no Calendar Application by default in Android. So the Uri may not work at all.
If, you are getting the first error message as "Failed to find provider info for com.android.calendar", you need to check the handset you are using to debug uses which Calendar URI.
To insert Time in Calendar event, you can use the following code example:
Calendar calendar = Calendar.getInstance();
calendar.getTimeInMillis();
cv.put("dtstart", ""+calendar.getTimeInMillis());
cv.put("dtend", ""+calendar.getTimeInMillis()+10000);

Categories

Resources