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.
Related
I have an issue related to calendar. I am using ContentResolver to add event to calendar. It works pretty well on any devices accept of samsung devices. When I am trying to add event to original samsung calendar nothing happens. But when I downloaded google calendar event added properly to it. Is samsung calendar is regular calendar and how can i use it like primary calendar? Thanks in advance for any help.
private long insertEventTask(Activity activity, CalEvent calEvent) {
ContentResolver contentResolver = activity.getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, calEvent.getStartMillis());
values.put(CalendarContract.Events.DTEND, calEvent.getEndMillis());
values.put(CalendarContract.Events.TITLE, calEvent.getTitle());
values.put(CalendarContract.Events.DESCRIPTION, calEvent.getDescription());
values.put(CalendarContract.Events.CALENDAR_ID, calEvent.getCalId());
values.put(CalendarContract.Events.EVENT_TIMEZONE, calEvent.getTimezone());
#SuppressLint("MissingPermission")
Uri uri = contentResolver.insert(CalendarContract.Events.CONTENT_URI, values);
Log.e("xxx", "uri is " + uri);
long eventId = Long.parseLong(uri.getLastPathSegment());
return eventId;
}
I'm adding events to a local calendar, this works fine with API < 24 (KitKat, Lollipop, Marshmallow), but I get issues with Google Calendar not able to open the events from my local calendar and returning "The requested event was not found"
Events are listed into Google Calendar, but cannot be opened, edited or deleted
Code to create the local calendar:
ContentResolver cr = context.getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, "My Calendar");
values.put(CalendarContract.Calendars.VISIBLE, 1);
values.put(CalendarContract.Calendars.NAME, "My Calendar");
values.put(CalendarContract.Calendars.CALENDAR_COLOR, BLACK_COLOR);
Uri updateUri = CalendarContract.Calendars.CONTENT_URI;
updateUri.buildUpon()
.appendQueryParameter(android.provider.CalendarContract.CALLER_IS_SYNCADAPTER, "false")
.build();
Uri uri = cr.insert(updateUri, values);
Code to create an event into the calendar:
ContentResolver cr = context.getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, start);
values.put(CalendarContract.Events.DTEND, end);
values.put(CalendarContract.Events.TITLE, title);
values.put(CalendarContract.Events.DESCRIPTION, description);
values.put(CalendarContract.Events.CALENDAR_ID, calID); // CalID = My Calendar Id
values.put(CalendarContract.Events.EVENT_TIMEZONE, "Australia/Sydney");
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
You need to provide an account name CalendarContract.Calendars.ACCOUNT_NAME and an account type in the ContentValues when you are creating the calendar
I have created some events and stored the data in sqlite table but not the system calendar. However, i would like to have an alert/reminder for these events, which is similar to other events that stored in the system.
With reference to the code below, the details of the event are put in the "CalendarAlerts" table and in the broadcast receiver, the alertCursor is used to find the event data with start time close to the current time.
The code worked well if the event is stored in the system with a "long" eventID. But when i try to put data of my sqlite event in "CalendarAlerts" with a "string" eventID. It shows that the data is inserted into the table successfully but i would not query back the result in the "Alert Receiver" class.
Searched google for a while and it seems that not many people are talking on the topic of "CalendarAlerts". Great if anyone would share the experience on this issue.
Setting the AlarmManager
Uri alertUri = CalendarAlerts.CONTENT_URI;
long alarmMillis = (long) mStart - (long)(min*60*1000);
ContentValues alertValues =AlertUtils.makeContentValues(eventIdentifier,mStart, mEnd,alarmMillis, 0);
context.getContentResolver().insert(alertUri, alertValues);
public static ContentValues makeContentValues(String eventId, long begin, long end,
long alarmTime, int minutes) {
ContentValues values = new ContentValues();
values.put(CalendarAlerts.EVENT_ID, eventId);
values.put(CalendarAlerts.BEGIN, begin);
values.put(CalendarAlerts.END, end);
values.put(CalendarAlerts.ALARM_TIME, alarmTime);
long currentTime = System.currentTimeMillis();
values.put(CalendarAlerts.CREATION_TIME, currentTime);
values.put(CalendarAlerts.RECEIVED_TIME, 0);
values.put(CalendarAlerts.NOTIFY_TIME, 0);
values.put(CalendarAlerts.STATE, CalendarAlerts.STATE_SCHEDULED);
values.put(CalendarAlerts.MINUTES, minutes);
return values;
}
AlertReceiver.java
Cursor alertCursor = cr.query(CalendarAlerts.CONTENT_URI, ALERT_PROJECTION,
(ACTIVE_ALERTS_SELECTION + currentMillis), ACTIVE_ALERTS_SELECTION_ARGS,
ACTIVE_ALERTS_SORT);
You can use this code which works for me :
ContentResolver cr = getActivity().getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, startMillis);
values.put(CalendarContract.Events.TITLE, title);
values.put(CalendarContract.Events.DESCRIPTION, "description");
TimeZone timeZone = TimeZone.getDefault();
values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
// default calendar
values.put(CalendarContract.Events.CALENDAR_ID, 1);
//for one hour
values.put(CalendarContract.Events.DURATION, "+P1H");
values.put(CalendarContract.Events.HAS_ALARM, 1);
// cr.delete(CalendarContract.Events.CONTENT_URI, null,null);
// insert event to calendar
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
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".
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