Android - Adding a calendar event without reminder - android

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";

Related

Calendar Provider with Android API 24+ "The requested event was not found"

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

Write and read ID of event Android

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.

Android: How to correctly use "CalendarAlerts" to display unique message in Alarms

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

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".

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
}

Categories

Resources