How To update Event of Calendar Programmatically in Android? - android

i am creating an android application in which i want to update existing Calendar event programmatic-ally. so please help me
following is the code of update event
ContentResolver cr = getActivity().getContentResolver();
ContentValues values = new ContentValues();
values.put (Events.CALENDAR_ID, 1);
values.put (Events.TITLE, titles);
values.put (Events.DTSTART, dtstart);
values.put (Events.DTEND, dtend);
int count = cr.update (Events.CONTENT_URI, values, Events._ID+" = "+eventId, null);
its not working please give me suggestion to solve it
thanks

private int UpdateCalendarEntry(int entryID) {
int iNumRowsUpdated = 0;
ContentValues event = new ContentValues();
event.put("title", "Changed Event Title");
event.put("hasAlarm", 1); // 0 for false, 1 for true
Uri eventsUri = Uri.parse(getCalendarUriBase()+"events");
Uri eventUri = ContentUris.withAppendedId(eventsUri, entryID);
iNumRowsUpdated = getContentResolver().update(eventUri, event, null,
null);
Log.i(DEBUG_TAG, "Updated " + iNumRowsUpdated + " calendar entry.");
return iNumRowsUpdated;
}

Related

Add events to Calendar programmatically

I'm using below code in Marshmellow device to add the event to the Calendar programmatically but it's not working. Any idea? I cannot see this event in the Calendar app.
long startMillis = 0;
long endMillis = 0;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startDate = null, endDate = null;
try{
startDate = simpleDateFormat.parse("2017-05-01 01:30:00");
startMillis = startDate.getTime();
endDate = simpleDateFormat.parse("2017-05-01 03:30:00");
endMillis = endDate.getTime();
}catch (ParseException e){
e.printStackTrace();
}
ContentResolver cr = this.getContentResolver();
ContentValues values = new ContentValues();
TimeZone timeZone = TimeZone.getDefault();
values.put(CalendarContract.Events.DTSTART, startMillis);
values.put(CalendarContract.Events.DTEND, endMillis);
values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
values.put(CalendarContract.Events.TITLE, "Hello Title");
values.put(CalendarContract.Events.DESCRIPTION, "Add events to Calendar");
values.put(CalendarContract.Events.CALENDAR_ID, 879);
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
PS: It works if only one gmail account is synced with the calendar app.
Am also find so many solutions finally i got solution and successfully calendar event added with Schedule event.
Step -1 > Enable Google calnedar API From google console.
Step - 2 > Add the permission in Androidmanifest.xml
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>
Step =3 > Add library for checking Permissions in your .gradle
compile 'com.karumi:dexter:4.1.0'
Dexter.withActivity(YoutActivityName.this)
.withPermission(Manifest.permission.WRITE_CALENDAR)
.withListener(new PermissionListener()
{
#Override
public void onPermissionGranted(PermissionGrantedResponse response) {
try {
int calenderId = -1;
String calenderEmaillAddress = "sathishmicit2012#gmail.com";
String[] projection = new String[]{
CalendarContract.Calendars._ID,
CalendarContract.Calendars.ACCOUNT_NAME};
ContentResolver cr = context.getContentResolver();
Cursor cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"), projection,
CalendarContract.Calendars.ACCOUNT_NAME + "=? and (" +
CalendarContract.Calendars.NAME + "=? or " +
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME + "=?)",
new String[]{calenderEmaillAddress, calenderEmaillAddress,
calenderEmaillAddress}, null);
if (cursor.moveToFirst()) {
if (cursor.getString(1).equals(calenderEmaillAddress)) {
calenderId = cursor.getInt(0);
}
}
long start2 = Calendar.getInstance().getTimeInMillis(); // 2011-02-12 12h00
long end2 = Calendar.getInstance().getTimeInMillis() + (4 * 60 * 60 * 1000); // 2011-02-12 13h00
String title = "This is my demo test with alaram with 5 minutes";
ContentValues cvEvent = new ContentValues();
cvEvent.put("calendar_id", calenderId);
cvEvent.put(CalendarContract.Events.TITLE, title);
cvEvent.put(CalendarContract.Events.DESCRIPTION, String.valueOf(start2));
cvEvent.put(CalendarContract.Events.EVENT_LOCATION, "Bhatar,Surat");
cvEvent.put("dtstart", start2);
cvEvent.put("hasAlarm", 1);
cvEvent.put("dtend", end2);
cvEvent.put("eventTimezone", TimeZone.getDefault().getID());
Uri uri = getContentResolver().insert(Uri.parse("content://com.android.calendar/events"), cvEvent);
// get the event ID that is the last element in the Uri
long eventID = Long.parseLong(uri.getLastPathSegment());
ContentValues values = new ContentValues();
values.put(CalendarContract.Reminders.MINUTES, 2);
values.put(CalendarContract.Reminders.EVENT_ID, eventID);
values.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALARM);
cr.insert(CalendarContract.Reminders.CONTENT_URI, values);
//Uri uri = cr.insert(CalendarContract.Reminders.CONTENT_URI, values);
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void onPermissionDenied(PermissionDeniedResponse response) {/* ... */}
#Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {/* ... */}
}).check();
Hello Calender ID is 879 you cant take it random. Calender ID must be from this URI content://com.android.calendar/calendars
int calenderId=-1;
String calenderEmaillAddress="xxx#gmail.com";
String[] projection = new String[]{
CalendarContract.Calendars._ID,
CalendarContract.Calendars.ACCOUNT_NAME};
ContentResolver cr = activity.getContentResolver();
Cursor cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"), projection,
CalendarContract.Calendars.ACCOUNT_NAME + "=? and (" +
CalendarContract.Calendars.NAME + "=? or " +
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME + "=?)",
new String[]{calenderEmaillAddress, calenderEmaillAddress,
calenderEmaillAddress}, null);
if (cursor.moveToFirst()) {
if (cursor.getString(1).equals(calenderEmaillAddress))
calenderId=cursor.getInt(0); //youre calender id to be insered in above your code
}

Adding events to android calendar failing

I am trying to add events to a user's calendar programatically. I have followed countless guides here on StackOverflow and elsewhere, but none of them will make that damned event show up in my calendar. Here is my current code:
public void saveGamesToCalendar() {
showCalendarPopup();
}
private void showCalendarPopup() {
final ContentResolver cr;
final Cursor result;
final Uri uri;
List<String> listCals = new ArrayList<String>();
final String[] projection = new String[] {CalendarContract.Calendars._ID, CalendarContract.Calendars.ACCOUNT_NAME, CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, CalendarContract.Calendars.NAME,};
uri = CalendarContract.Calendars.CONTENT_URI;
cr = context.getContentResolver();
result = cr.query(uri, projection, null, null, null);
if(result.getCount() > 0 && result.moveToFirst()) {
do {
listCals.add(result.getString(result.getColumnIndex(CalendarContract.Calendars.NAME)));
} while(result.moveToNext());
}
CharSequence[] calendars = listCals.toArray(new CharSequence[listCals.size()]);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Calendar to use:");
builder.setItems(calendars, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,int itemCal) {
Log.e("SettingsActivity", "CalendarID: " + itemCal);
loopThroughGames(itemCal);
};
});
AlertDialog alert = builder.create();
alert.show();
}
private void loopThroughGames(int whichCalendar) {
ArrayList<Model_Game> games = memoryManager.getGames();
// for(Game e: games)
Log.e("Game", games.get(101).toString());
addGameToCalendar(games.get(101), whichCalendar);
Log.e("ID", "" + whichCalendar);
}
private void addGameToCalendar(Model_Game game,int whichCalendar) {
ContentResolver cr = context.getContentResolver();
ContentValues values = new ContentValues();
try {
values.put(CalendarContract.Events.CALENDAR_ID, whichCalendar);
values.put(CalendarContract.Events.TITLE, "Hockey- " + game.getLeague() + ": " + game.getTeamH() + " vs " + game.getTeamA());
values.put(CalendarContract.Events.EVENT_LOCATION, "Twin Rinks Ice Arena- " + game.getRink() + " Rink");
values.put(CalendarContract.Events.DTSTART, "" + game.getCalendarObject().getTimeInMillis());
//values.put(CalendarContract.Events.DTEND, "" + game.getCalendarObject().getTimeInMillis() + 5400000);
values.put(CalendarContract.Events.DURATION, "" + 5400000);
values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
cr.insert(CalendarContract.Events.CONTENT_URI, values);
} catch(Exception e) {
Log.e("Error", e.getMessage());
toast(e.getMessage());
}
}
Basically what is going on is that the user presses an action bar button, which calls the saveGamesToCalendar() method, which calls the ShowCalendarPopup() method. This method shows a standard dialog with the list of the user's calendars, and when the user selects one of them, the loopThroughGames() method is called with the ID of the chosen calendar as a parameter. This method will, when everything is working, write all the user's games to the calendar, but for testing purposes it only writes one. The addGameToCalendar() method takes a game object, which holds lots of values like time, title, location, date, etc., and inserts it into the calendar using the standard way outlined in many different places on the web.
I am getting no error messages with this code (other than the ones I send to the error log myself) so I have no idea why this code isn't working. No errors, just the games never show up in my calendar. I have the permissions set correctly in the manifest so I know that isn't the problem.
Does anyone have a solution to this annoying problem? Thanks for your help!
[RE-EDIT]]
This is what I did and it works like a charm..
Cursor cursor = null ;
String[] projection = new String[] {
CalendarContract.Calendars._ID,
CalendarContract.Calendars.ACCOUNT_NAME,};
ContentResolver cr = context2.getContentResolver();
cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"), projection, null, null, null);
if ( cursor.moveToFirst() ) {
final String[] calNames = new String[cursor.getCount()];
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();
}
}
try {
ContentValues values = new ContentValues();
// int apiLevel = android.os.Build.VERSION.SDK_INT;
// if(apiLevel<14){
// values.put("visibility", 0);
//
// }
values.put(Events.DTSTART, stTime);
values.put(Events.DTEND, enTime);
values.put(Events.TITLE, category);
values.put(Events.DESCRIPTION, description);
values.put(Events.CALENDAR_ID, calIds[0]);
values.put(Events.EVENT_LOCATION,place);
values.put(Events.EVENT_TIMEZONE,tZone);
mInsert = cr.insert(CalendarContract.Events.CONTENT_URI, values);
Toast.makeText(context2, "Event added Successfully",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(context2, "Exception: " + e.getMessage(),
Toast.LENGTH_SHORT).show();
}
protected void eventAdding() {
try
{
Calendar beginTime = Calendar.getInstance();
beginTime.set(2013, 11, 20, 8,0 );
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2013, 11, 20, 8, 50);
endMillis = endTime.getTimeInMillis();
ContentResolver cr = this.getContentResolver();
ContentValues values = new ContentValues();
values.put(Events.DTSTART, startMillis);
values.put(Events.DTEND, endMillis);
values.put(Events.TITLE, "Hello");
values.put(Events.CALENDAR_ID, 1);
values.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
if (android.os.Build.VERSION.SDK_INT <= 7) {
uri = Uri.parse("content://calendar/events");
} else
{
uri = Uri.parse("content://com.android.calendar/events");
}
Uri urievent = cr.insert(uri, values);
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//event = Utility.readCalendarEvent(CalnderEvent.this);
}
This works and is much easier to understand:
long startTime = calSet.getTimeInMillis();
Intent intent = new Intent(Intent.ACTION_INSERT).setData(Events.CONTENT_URI)
.setData(Events.CONTENT_URI)
.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, startTime)
.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, startTime + (length*3600000))
.putExtra(Events.TITLE, examName)
.putExtra(Events.EVENT_LOCATION, venue)
.putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY)
.putExtra(Events.HAS_ALARM, 1);
startActivity(intent);

Calendar sync with custom add account in android

I want to add events in calendar, i am able to add account but i don`t know how to sync with calendar.
should i create a new calendar id ?
how to verify event is created on calendar?
Check out this Google code project which will guide you.
In that project there is a method which will give you id of account id associated with Calender.
private int ListSelectedCalendars() {
int result = 0;
String[] projection = new String[] { "_id", "name" };
String selection = "selected=1";
String path = "calendars";
Cursor managedCursor = getCalendarManagedCursor(projection, selection,
path);
if (managedCursor != null && managedCursor.moveToFirst()) {
Log.i(DEBUG_TAG, "Listing Selected Calendars Only");
int nameColumn = managedCursor.getColumnIndex("name");
int idColumn = managedCursor.getColumnIndex("_id");
do {
String calName = managedCursor.getString(nameColumn);
String calId = managedCursor.getString(idColumn);
Log.i(DEBUG_TAG, "Found Calendar '" + calName + "' (ID="
+ calId + ")");
// You have to give email id in below line, right now i puted my email id
if (calName != null && calName.contains("vimalrajpara2006#gmail.com")) {
result = Integer.parseInt(calId);
}
} while (managedCursor.moveToNext());
} else {
Log.i(DEBUG_TAG, "No Calendars");
}
return result;
}
based on that id you can add events as well search also.
private Uri MakeNewCalendarEntry(int calId/*Value received from ListSelectedCalendars function*/) {
ContentValues event = new ContentValues();
event.put("calendar_id", calId);
event.put("title", "Today's Event [TEST]");
event.put("description", "2 Hour Presentation");
event.put("eventLocation", "Online");
long startTime = System.currentTimeMillis() + 1000 * 60 * 60;
long endTime = System.currentTimeMillis() + 1000 * 60 * 60 * 2;
event.put("dtstart", startTime);
event.put("dtend", endTime);
event.put("allDay", 0); // 0 for false, 1 for true
event.put("eventStatus", 1);
event.put("visibility", 0);
event.put("transparency", 0);
event.put("hasAlarm", 0); // 0 for false, 1 for true
Uri eventsUri = Uri.parse(getCalendarUriBase()+"events");
Uri insertedUri = getContentResolver().insert(eventsUri, event);
return insertedUri;
}
For more functionality check out above mention Google code project.
this is work for me remember I am working on android 4.0
void createCalendar(Datahelper dh, Context mContext, Account account)
{
final ContentValues v = new ContentValues();
v.put(CalendarContract.Calendars.NAME,account.name);
v.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, account.name);
v.put(CalendarContract.Calendars.ACCOUNT_NAME, account.name);
v.put(CalendarContract.Calendars.ACCOUNT_TYPE, account.type);
v.put(CalendarContract.Calendars.CALENDAR_COLOR, Color.GREEN);
v.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL,Calendars.CAL_ACCESS_OWNER);
v.put(CalendarContract.Calendars.OWNER_ACCOUNT, account.name);
v.put(CalendarContract.Calendars._ID, 123);// u can give any id there and use same id any where u need to create event
v.put(Calendars.SYNC_EVENTS, 1);
v.put(Calendars.VISIBLE, 1);
Uri creationUri = asSyncAdapter(Calendars.CONTENT_URI, account.name, account.type);
Uri calendarData = mContext.getContentResolver().insert(creationUri, v);
long id = Long.parseLong(calendarData.getLastPathSegment());
}
private Uri asSyncAdapter(Uri uri, String account, String accountType)
{
return uri.buildUpon().appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true").appendQueryParameter (Calendars.ACCOUNT_NAME,account) .appendQueryParameter(Calendars.ACCOUNT_TYPE, accountType) .build();
}
ContentValues values = new ContentValues();
String eventTitle = eventsubject[i];
values.put(Events.DTSTART, startMillis);
values.put(Events.HAS_ALARM, 1);
values.put(Events.DTEND, endMillis);
values.put(Events.EVENT_COLOR, Color.BLUE);
values.put(Events.TITLE, eventTitle);
values.put(Events.DESCRIPTION, "");
values.put(Events.CALENDAR_ID, calendarId1);
values.put(Events.EVENT_TIMEZONE, TimeZone.getDefault() .getID());
Uri uri = cr.insert(Events.CONTENT_URI, values);
long eventID = Long.parseLong(uri.getLastPathSegment());
ContentValues reminders = new ContentValues();
reminders.put(Reminders.EVENT_ID, eventID);
reminders.put(Reminders.METHOD, Reminders.METHOD_ALERT);
reminders.put(Reminders.MINUTES, 2);
Uri uri2 = cr.insert(Reminders.CONTENT_URI, reminders);
Log.e("Reminder", "addreminder" + uri2);

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

sync event added programmatically with google calendar in android

I am trying to add an event to the android calendar, and I specify that the event will be added to the gmail calendar in order to sync with the Google calendar automatically.
The problem is events added programmatically don't sync with Google calendar, but if I add it manual on the phone it does sync with Google calendar. I don't know why.
This is the code that I use to add the event:
ArrayList<MyCalendar> calendars = new ArrayList<MyCalendar>();
String[] projection = new String[] { "_id", "name" };
Uri calUri = getCalendarURI(false);
Cursor managedCursor = managedQuery(calUri, projection, "selected=1",
null, null);
String calName = null;
String calId = null;
if (managedCursor.moveToFirst()) {
int nameColumn = managedCursor.getColumnIndex("name");
int idColumn = managedCursor.getColumnIndex("_id");
do {
calName = managedCursor.getString(nameColumn);
calId = managedCursor.getString(idColumn);
calendars.add(new MyCalendar(Integer.parseInt(calId), calName));
} while (managedCursor.moveToNext());
}
Toast.makeText(getBaseContext(), calName + " " + calId,
Toast.LENGTH_LONG).show();
Calendar cal = Calendar.getInstance();
ContentValues event = new ContentValues();
event.put("calendar_id", 2);
event.put("title", "Test Event2");
event.put("description", "Hiii Buddy");
long startTime = cal.getTimeInMillis();
long endTime = cal.getTimeInMillis() + 60 * 60 * 1000;
event.put("dtstart", startTime);
event.put("dtend", endTime);
event.put("allDay", 0);
event.put("eventStatus", 1);// tentative 0, confirmed 1 canceled 2
event.put("visibility", 3);// default 0 confidential 1 private 2
// public 3
event.put("transparency", 0);// opaque 0 transparent 1
event.put("hasAlarm", 1); // 0 false, 1 true
Uri eventsUri = getCalendarURI(true);
Uri url = getContentResolver().insert(eventsUri, event);
So the event successfully added to calendar but it doesn't show up in the Google calendar at the web (don't sync) but if I add the event manually it does sync !!!
You can sync your event after adding it by this function,It's worked for me(in API 8 and later):
public static void syncCalendar(Context context, String calendarId) {
ContentResolver cr = context.getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
values.put(CalendarContract.Calendars.VISIBLE, 1);
cr.update(
ContentUris.withAppendedId(getCalendarUri(),
Long.parseLong(calendarId)), values, null, null);
}
Try this code to insert an event into the android calendar as well as google calendar:
ContentValues values = new ContentValues();
cal_id = String.valueOf(p1);
values.put("calendar_id", p1);
values.put("title", title1);
values.put("allDay", 0);
values.put("dtstart", settime);
values.put("dtend", cal.getTimeInMillis()+60*60*1000);
values.put("description", desc1);
values.put("visibility", 0);
values.put("transparency", 0);
values.put("hasAttendeeData", 1);
values.put("hasAlarm", 0);
event = cr.insert(EVENTS_URI, values);
event1=event;
dat1 = event.toString();
long id=-1;
if (event != null)
{
id = Long.parseLong(event.getLastPathSegment());
ContentValues values1 = new ContentValues();
values1.put("event_id", id);
values1.put("method", 1); //METHOD_ALERT
Uri reminder = Uri.parse(getCalendarUriBase(this) + "reminders");
this.getContentResolver().insert(reminder, values1);
if(s.length() > 0 || partmail.length() > 0)
{
//REQUIRES FOLLOWING CODE
This code is used to add the event sync'ed to the google calendar
ContentValues attendees = new ContentValues();
attendees.put("event_id", id);
attendees.put("attendeeEmail", partmail1);
attendees.put("attendeeRelationship", 2);//RELATIONSHIP_ATTENDEE
attendees.put("attendeeStatus", 3); //ATTENDEE_STATUS_INVITED
attendees.put("attendeeType", 1); //TYPE_REQUIRED
id1=(int)id;
alarmid = (int) id;
Uri attendeesUri = null;
if (Integer.parseInt(Build.VERSION.SDK) >= 8 )
{
attendeesUri = Uri.parse("content://com.android.calendar/attendees");
}
else if(Integer.parseInt(Build.VERSION.SDK) < 8)
{
attendeesUri = Uri.parse("content://calendar/attendees");
}
this.getContentResolver().insert(attendeesUri, attendees);
Toast.makeText(this, "Task Scheduled Successfully", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this, "Could not create Task!", Toast.LENGTH_SHORT);
}
// reminder insert
Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(this) + "reminders");
values = new ContentValues();
values.put( "event_id", id);
values.put( "method", 1 );
values.put( "minutes", 0 );
cr.insert( REMINDERS_URI, values );
Get Calendar's Uri since it differs for API levels such as till 8, greater than 8 and greater than 11.
private String getCalendarUriBase(Context con) {
String calendarUriBase = null;
Uri calendars = Uri.parse("content://calendar/calendars");
Cursor managedCursor = null;
try {
managedCursor = managedQuery(calendars, null, null, null, null);
} catch (Exception e) {
// eat
}
if (managedCursor != null) {
calendarUriBase = "content://calendar/";
} else {
calendars = Uri.parse("content://com.android.calendar/calendars");
try {
managedCursor = managedQuery(calendars, null, null, null, null);
} catch (Exception e) {
// statement to print the stacktrace
}
if (managedCursor != null) {
calendarUriBase = "content://com.android.calendar/";
}
}
return calendarUriBase;
}
Use the google calender api provided here
The above reference is somewhat general as Android doesn’t provide an official API. So, another solution more android specific is An Android Tutorial–Programming with Calendar
which contains a working example too. Also, be careful at Permission Declaration like READ_CALENDAR , WRITE_CALENDAR

Categories

Resources