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".
Related
I'd like to add an event and get it back programmatically in android. I have two option to add an event to the calender but neither of them good at adding ID to the event. I set the number of the ID into 32 but when I create an event it's ID is growing up. Then how can I add the ID I want?
Option1:
public void InsertAnEvent2(){
Calendar calendarEvent = Calendar.getInstance();
Intent i = new Intent(Intent.ACTION_EDIT);
i.setType("vnd.android.cursor.item/event");
i.putExtra("beginTime", calendarEvent.getTimeInMillis());
i.putExtra("allDay", true);
i.putExtra("rule", "FREQ=YEARLY");
i.putExtra("endTime", calendarEvent.getTimeInMillis() + 60 * 60 * 1000);
i.putExtra("title", "Eskuvo");
i.putExtra("calendar_id",32);
startActivity(i);
}
Option2:
public void Mindencalendar(){
ContentResolver cr = getActivity().getContentResolver();
Calendar calendarEvent = Calendar.getInstance();
Log.d("i'm","here1");
long idk[] = new long[10];
idk[0] = cu.addEventToCalender(cr,"a","b","c",5,calendarEvent.getTimeInMillis());
Log.d("id","id"+idk[0]);
}
public class CalendarUtils {
public static long addEventToCalender(ContentResolver cr, String title, String addInfo, String place, int status,
long startDate) {
String eventUriStr = "content://com.android.calendar/events";
ContentValues event = new ContentValues();
event.put("calendar_id", 32);
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());
return eventID;
}
}
But maybe the problem is how I try to read these events. Here is my code:
public void ReadFromCalendar(){
Uri EVENTS_URI = Uri.parse("content://com.android.calendar/" + "events");
ContentResolver cr = getActivity().getContentResolver();
Cursor cursor;
cursor = cr.query(EVENTS_URI, null, null, null, null);
//int a = cursor.getCount();
while(cursor.moveToNext()) {
long id = cursor.getLong(cursor.getColumnIndex("calendar_id"));
Log.d("TAG", "ID: " + id);
Uri eventUri = ContentUris.withAppendedId(EVENTS_URI, id);
}
cursor.close();
}
I don't know where do I make a mistake. If anyone has an idea please response.
Can you post the database structure, one of the first thoughts was that your calendar_id column has the auto-increment enabled. In that case you can add another column to your table or alter the existing one, my recommendation is to add another one. But let's see the columns table details.
I'm using the following function to add an event to calendar:
public String addEventToCalendar(long startDate, long endDate, String recurrenceRule, boolean isAllDay, String title, String description, String location, long calendarID) {
ContentResolver cr = context.getContentResolver();
ContentValues values = new ContentValues();
TimeZone timeZone = TimeZone.getDefault();
values.put(CalendarContract.Events.DTSTART, startDate);
values.put(CalendarContract.Events.DTEND, endDate);
values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
if (recurrenceRule != null)
values.put(CalendarContract.Events.RRULE, recurrenceRule);
values.put(CalendarContract.Events.TITLE, title);
values.put(CalendarContract.Events.DESCRIPTION, description);
values.put(CalendarContract.Events.CALENDAR_ID, calendarID);
values.put(CalendarContract.Events.ALL_DAY, isAllDay);
values.put(CalendarContract.Events.EVENT_LOCATION, location);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
return null; // we don't have the right permissions
}
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
String eventID = uri.getLastPathSegment();
return eventID;
}
It works, but the resulting calendar event, have a 30 minutes reminder! I'm not able to figure out why. Any clue please?
Thanks a lot.
It seems like a default reminder is added after you insert your calendar event.
What you could try is to check if there are any reminder associated with your event right after you inserted it. And delete it if so.
CalendarContract.Reminders.query(contentResolver, eventId, projection)
will give you a list of reminder associated with the eventId
If the Cursor contains any reminder you can delete it with :
getContentResolver().delete(ContentUris.withAppendedId(CalendarContract.Reminders.CONTENT_URI, reminderId), null, null);
docs : http://developer.android.com/reference/android/provider/CalendarContract.Reminders.html
Try to add this
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.HAS_ALARM, 0);
Whether the event has an alarm or not. Column name.
Type: INTEGER (boolean)
public static final String HAS_ALARM = "hasAlarm";
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.
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
}
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