Adding event to calendar - android

I have this code for adding an event to the calendar:
long calID = 3;
long startMillis = 0;
long endMillis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2013, 3, 23, 7, 30);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2013, 3, 24, 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, "My Test");
values.put(Events.DESCRIPTION, "My Calendar Test");
values.put(Events.CALENDAR_ID, calID);
values.put(Events.EVENT_TIMEZONE, "Israel/tel-aviv");
Uri uri = cr.insert(Events.CONTENT_URI, values);
// get the event ID that is the last element in the Uri
long eventID = Long.parseLong(uri.getLastPathSegment());
It works correctly on my Galaxy S3, but on my Galaxy S2 it doesn't work - nothing happens - this is despite both having the same Android version - 4.1.2

Related

added events are not showing in calendar app in android

I am using calender provider to insertEvent in google calender . The problem is I am getting the Query result as Uri without any exception and event Id also . But the Event i am adding is not showing in calender app.Can anyone Help me . And i also wanted to set reminder for my event . Below is the code i am using to add event .
public void addNewEvent() {
long startMillis = 0;
long endMillis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2016, 4, 1, 7, 30);
Log.e("startTime",new SimpleDateFormat("MM:dd:yyyy").format(beginTime.getTimeInMillis()));
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2016, 4, 1, 8, 56);
endMillis = endTime.getTimeInMillis();
// Insert Event
Log.e("endTime",new SimpleDateFormat("MM:dd:yyyy").format(endTime.getTimeInMillis()));
ContentResolver cr = activity.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, "Going for a ride");
values.put(CalendarContract.Events.DESCRIPTION, "Event desc");
values.put(CalendarContract.Events.CALENDAR_ID, 39);
values.put(CalendarContract.Events.EVENT_LOCATION,"Malta");
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
// Retrieve ID for new event
long eventID = Long.parseLong(uri.getLastPathSegment());
setReminder(cr, eventID, 100);
Log.e("eventId",eventID+"");
}
public void setReminder(ContentResolver cr, long eventID, int timeBefore) {
try {
ContentValues values = new ContentValues();
values.put(CalendarContract.Reminders.MINUTES, timeBefore);
values.put(CalendarContract.Reminders.EVENT_ID, eventID);
values.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
Uri uri = cr.insert(CalendarContract.Reminders.CONTENT_URI, values);
Cursor c = CalendarContract.Reminders.query(cr, eventID,
new String[]{CalendarContract.Reminders.MINUTES});
if (c.moveToFirst()) {
Log.e("Reminder Uri",uri.toString());
Log.e("","calendar"
+ c.getInt(c.getColumnIndex(CalendarContract.Reminders.MINUTES)));
}
c.close();
} catch (Exception e) {
e.printStackTrace();
}
}
above code helps in adding events to phone calendar only.if you want to add events to google calendar then you have to use google calendar api. when adding events to phone calendar i have set calendar id to "1".

android calendar events removed when sync up google calendar

I have created events in android calendar pragmatically, these events are removed by default when my android calendar syncup with google calendar. Please note my code in below
Calendar beginTime = Calendar.getInstance();
beginTime.setTimeInMillis(sTime.getTime());
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.setTimeInMillis(eTime.getTime());
endMillis = endTime.getTimeInMillis();
ContentResolver cr = context.getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.TITLE, title);
values.put(CalendarContract.Events.DTSTART, startMillis);
values.put(CalendarContract.Events.DTEND, endMillis);
values.put(CalendarContract.Events.CALENDAR_ID, calID);
values.put(CalendarContract.Events.EVENT_TIMEZONE, CalendarContract.Calendars.CALENDAR_TIME_ZONE);
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
I want to keep my events in anroid calendar, when syncup with google calendar. How can i do this, Please help me.
All calendars have field ACCESS_LEVEL. If you write to a calendar without write access, Google will delete the event on syncing.

Event not being added to Calendar on some Android phones

I am using below code to add events to calendar on android
public void addEvent(String datetime) {
String eventdate;
{
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm");
final Calendar cal = Calendar.getInstance();
try {
cal.setTime(formatter.parse(datetime));
eventdate = cal.get(Calendar.YEAR)+"/"+cal.get(Calendar.MONTH)+"/"+cal.get(Calendar.DAY_OF_MONTH)+" "+cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE);
//Log.e("Event date ", eventdate);
} catch (Exception e) {
Log.e("Catch ", "",e);
}
ContentValues event = new ContentValues();
event.put("calendar_id", 3);
event.put("_id", eventid);
event.put("title", mytitle);
event.put("description", mydescription);
event.put("eventTimezone", TimeZone.getDefault().getID());
event.put("dtstart", cal.getTimeInMillis());
event.put("dtend", cal.getTimeInMillis()+60*60*1000);
event.put("hasAlarm", 1); // 0 for false, 1 for true
String eventUriString = "content://com.android.calendar/events";
Uri eventUri = getApplicationContext()
.getContentResolver()
.insert(Uri.parse(eventUriString), event);
System.out.println("event"+eventUri);
}
the following code adds event to my HTC phone running android lollipop but it returns null on Phones like Micromax and Samsung running android Jellybean. What can be the reason for this behavior? Do I need to turn anything on from settings?
try using the constants provided by the Events class.
ContentResolver cr = yourContext.getContentResolver();
ContentValues event = new ContentValues();
event.put(Events.DTSTART, cal.getTimeInMillis());
event.put(Events.DTEND, cal.getTimeInMillis() + 60 * 60 * 1000);
event.put(Events.TITLE, mytitle);
event.put(Events.DESCRIPTION, mydescription);
event.put(Events.CALENDAR_ID, calID);
event.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
...
Uri uri = cr.insert(Events.CONTENT_URI, event);
The Event id of this inserted event can be get from this method
long eventID = Long.parseLong(uri.getLastPathSegment());
Check the title Adding Events in Calendar Provider
http://developer.android.com/guide/topics/providers/calendar-provider.html
If you want to insert an _id to the Event, you should check if it is already there
Uri event = ContentUris.withAppendedId(Events.CONTENT_URI, _id);
Cursor cursor = managedQuery(event, null, null, null);
if (cursor.getCount() == 1) {
//the event exists.. so may be you want to update it
} else {
// you can insert your id
}

Android Insert Calendar Intent - Without alarm/reminder

I want to insert a calendar event via intent. But the "add event"-Activity should not be prefilled with a reminder/alarm.
Intent intent = new Intent(Intent.ACTION_INSERT)
.setData(Events.CONTENT_URI)
.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis())
.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis())
.putExtra(Events.TITLE, title)
.putExtra(Events.DESCRIPTION, description)
.putExtra(Events.HAS_ALARM, false)
.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true);
This intent will start the calendar's "add event"-activity prefilled with some data. However, although I set Events.HAS_ALARM to false, the activity is pre-populated with a reminder (tested on Android ICS).
What's even worse, the reminder is prepopulated to 10 minutes before the event, which in case of an all-day event is really bad. Who wants to be reminded at 11.50 pm of an event the next day?
What I am missing here?
I never tried your technique above. Here is the code snippet I use to save Calendar.
public static void saveCalendar(Context ctx, String title,
String description, String location, Calendar cal_start,
Calendar cal_end) {
// look for calendar
Cursor cursor = ctx.getContentResolver()
.query(Uri.parse("content://com.android.calendar/calendars"),
new String[] { "_id", "displayname" }, "selected=1",
null, 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();
// put calendar event
ContentValues event = new ContentValues();
event.put("calendar_id", CalIds[0]);
event.put("title", title);
event.put("description", description);
event.put("eventLocation", location);
event.put("dtstart", cal_start.getTimeInMillis());
event.put("dtend", cal_end.getTimeInMillis());
event.put("hasAlarm", 1);
Uri eventsUri = Uri.parse("content://com.android.calendar/events");
Uri newEvent = ctx.getContentResolver().insert(eventsUri, event);
// put alarm reminder for an event, 2 hours prior
long eventID = Long.parseLong(newEvent.getLastPathSegment());
ContentValues cv_alarm = new ContentValues();
cv_alarm.put("event_id", eventID);
cv_alarm.put("method", 1);
cv_alarm.put("minutes", 120);
ctx.getContentResolver()
.insert(Uri.parse("content://com.android.calendar/reminders"),
cv_alarm);
}
It you don't want the alarm/reminder set 0 to hasAlarm and don't put the codes to add the alarm. It works for me.
The column HAS_ALARM expects an Integer boolean of 0 or 1.
intent.putExtra(Events.HAS_ALARM, 0);

Android 2.2 - Why I can not see the event I added into Calendar?

I am new to android development world. I want to add one event into my native calendar, I can see the operation successfully, however when I go to Calendar I can not see that. My codes are below
String [] projection = new String [] {"_id", "name"};
Uri calendars = Uri.parse("content://com.android.calendar/calendars");
Cursor c = managedQuery(calendars, projection, "selected=1", null, null);
if(c.moveToFirst()){
String calName;
String calID;
int nameColumn = c.getColumnIndex("name");
int idColumn = c.getColumnIndex("_id");
calName = c.getString(nameColumn);
calID = c.getString(idColumn);
Time start = new Time("20110416T090000");
Time end = new Time("20110416T100000");
ContentValues values = new ContentValues();
values.put("calendar_id", calID);
values.put("title", "Event Title");
values.put("description", "test d");
values.put("eventLocation", "Melbourne");
values.put("dtstart", start.toMillis(true));
values.put("dtend", end.toMillis(true));
values.put("allDay", 0);
values.put("eventStatus", 1);
values.put("transparency", 0);
values.put("visibility", 0);
values.put("hasAlarm", 1);
Uri events = Uri.parse("content://com.android.calendar/events");
Uri result = getContentResolver().insert(events, values);
I use Motorola unit. Can anybody point out why I am failed? Thanks a lot.
I have used the following ContentValues in my own projects and been successful:
ContentValues cv = new ContentValues();
cv.put("calendar_id", calendarid);
cv.put("title", "sometitle");
cv.put("dtstart", ""+calendarfrom.getTimeInMillis());
cv.put("dtend", ""+calendarto.getTimeInMillis());
cv.put("hasAlarm", 0);
Uri newevent = getContentResolver().insert(Uri.parse("content://calendar/events"), cv);
I suspect one of the contentvalues you are providing is causing the failure. Try my simplified example first.

Categories

Resources