Actually i am get stuck in a big problem..I have created an app from which i can save event in my device calendar..Now when i save new events from my app in my device calendar it will always delete the events save previously by my app and save a new event and so on..so all works fine..now the big problem is that while deleting it will delete all the events of the calendar that are present in the device calendar including the events that are save by my app..so what i want is to delete only that event that are put by my app while inserting new event from my app not that are already present or which are directly assigned by me in device calendar..so can anyone please help me out to resolve this problem..the code i have use for inserting and deleting are..
Resources res = c.getResources();
Uri EVENTS_URI = Uri.parse("content://com.android.calendar/" + "events");
Uri REMINDERS_URI = Uri.parse("content://com.android.calendar/" + "reminders");
ContentResolver cr = c.getContentResolver();
Uri uri= ContentUris.withAppendedId(EVENTS_URI, 1);
deleteEvent(cr, Resources res = c.getResources();
Uri EVENTS_URI = Uri.parse("content://com.android.calendar/" + "events");
Uri REMINDERS_URI = Uri.parse("content://com.android.calendar/" + "reminders");
ContentResolver cr = c.getContentResolver();
//Deleting event from device calendar before saving new event
deleteEvent(cr, EVENTS_URI, 1);
//saving new data to calendar
ContentValues values = new ContentValues();
values.put("calendar_id", 1);
values.put("title", str);
values.put("description", m_strDescription);
values.put("dtstart", cal.getTimeInMillis());
values.put("dtend", cal.getTimeInMillis());
values.put("hasAlarm", 1);
Uri event = cr.insert(EVENTS_URI, values);
values = new ContentValues();
values.put("event_id", Long.parseLong(event.getLastPathSegment()));
values.put("method", 1);
values.put("minutes", 10);
cr.insert(REMINDERS_URI, values);
Functions for deleting event
private void deleteEvent(ContentResolver resolver, Uri eventsUri, int calendarId)
{
Cursor cursor;
if (android.os.Build.VERSION.SDK_INT <= 7)
{
cursor = resolver.query(eventsUri, new String[]{ "_id" }, "Calendars_id=" + calendarId, null, null);
}
else
{
cursor = resolver.query(eventsUri, new String[]{ "_id" }, "calendar_id=" + calendarId, null, null);
}
while(cursor.moveToNext())
{
long eventId = cursor.getLong(cursor.getColumnIndex("_id"));
resolver.delete(ContentUris.withAppendedId(eventsUri, eventId), null, null);
}
cursor.close();
}
The code you are using is deleting EVERY event: you need to save the ID of the event you create and only delete that one event. When you do this:
cr.insert(REMINDERS_URI, values);
change that to this:
Uri u = cr.insert(REMINDERS_URI, values);
This will save the URI of the event you create. You can then pass that URI into your deleteEvent method to only delete that one event, rather than all events.
Related
I have to invite multiple members to the google calendar while creating the event and later on when event is created then he can alter the invited member from google calendar .
Here is the code to insert the multiple invitee : but can't able to insert the attendee. but i am able to insert the remaining data.
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
ArrayList<String> al = new ArrayList<String>();
al.add("ashis#gmail.com");
al.add("sonam#gmail.com");
intent.putStringArrayListExtra(CalendarContract.Attendees.ATTENDEE_EMAIL,al);
intent.putExtra(CalendarContract.Events.EVENT_LOCATION, locationName);
Update google Calendar : Here i am able to update the value of the endTime but attendee is not been updated .
public void updateEvent(long eventID,long endTime,String calendarEventID){
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("content://com.android.calendar/events/" + calendarEventID));
ContentResolver cr = getActivity().getContentResolver();
Uri eventUri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventID);
ContentValues event = new ContentValues();
event.put(CalendarContract.Events.DTEND, endTime);
event.put(CalendarContract.Attendees.ATTENDEE_EMAIL, "sunil#gmail.com");
cr.update(eventUri, event, null, null);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getActivity().startActivity(intent);
}
According to the Android docs in Adding Attendees, you ought to use put instead of like:
Here is an example that adds a single attendee to an event. Note that the EVENT_ID is required:
long eventID = 202;
...
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(Attendees.ATTENDEE_NAME, "Trevor");
values.put(Attendees.ATTENDEE_EMAIL, "trevor#example.com");
values.put(Attendees.ATTENDEE_RELATIONSHIP, Attendees.RELATIONSHIP_ATTENDEE);
values.put(Attendees.ATTENDEE_TYPE, Attendees.TYPE_OPTIONAL);
values.put(Attendees.ATTENDEE_STATUS, Attendees.ATTENDEE_STATUS_INVITED);
values.put(Attendees.EVENT_ID, eventID);
Uri uri = cr.insert(Attendees.CONTENT_URI, values
);
This is how I create calendar events from within my app:
for(CalendarEventDescriptor calendarEventDescriptor : calendarEventDescriptors.values()) {
if(calendarEventDescriptor.startMilliseconds>now){
values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, calendarEventDescriptor.startMilliseconds);
values.put(CalendarContract.Events.DTEND, calendarEventDescriptor.endMilliseconds);
values.put(CalendarContract.Events.TITLE, calendarEventDescriptor.title);
values.put(CalendarContract.Events.DESCRIPTION, calendarEventDescriptor.description);
values.put(CalendarContract.Events.CALENDAR_ID, 1);
values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone);
uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
calendarEventDescriptor.eventId = Long.parseLong(uri.getLastPathSegment());
}
}
At the time or writing, I store an array of all the event Ids that I've created, so that when the user flicks a switch, I loop through them and delete them from the Calendar.
for(long eventId : eventIds) {
if(eventId>0){
Uri deleteUri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventId);
rowsDeleted += application.getContentResolver().delete(deleteUri, null, null);
}
}
It occurred to me that it might be possible to put a custom value for one of the CalendarContract.Events. columns so that I can do the deletion for all of the events at once, and that I don't have to remember their ids (I always delete them all, never delete certain ones)
Is that possible and which CalendarContract.Events. column should I use and how do I do the deletion then?
Nice question! I agree, that extra-property in ContentValues is the way to go in this case.
I've done it by re-using CalendarContract.Events.CUSTOM_APP_PACKAGE, as something invisible for the user and, so far, no side effects found:
So I'm creating Events, like you do:
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.CALENDAR_ID, 1);
values.put(CalendarContract.Events.DTSTART, ...);
values.put(CalendarContract.Events.DTEND, ...);
values.put(CalendarContract.Events.TITLE, ...);
values.put(CalendarContract.Events.DESCRIPTION, ....);
values.put(CalendarContract.Events.CUSTOM_APP_PACKAGE, getApplicationContext().getPackageName());
values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
cr.insert(CalendarContract.Events.CONTENT_URI, values);
And then once I need to delete all of them, I call:
Cursor cursor = cr
.query(CalendarContract.Events.CONTENT_URI,
new String[] { CalendarContract.Events._ID, CalendarContract.Events.CUSTOM_APP_PACKAGE },
null, null, null);
cursor.moveToFirst();
String idsToDelete = "";
for (int i = 0; i < cursor.getCount(); i++) {
// it might be also smart to check CALENDAR_ID here
if (getApplicationContext().getPackageName().equals(cursor.getString(1))) {
idsToDelete += String.format("_ID = %s OR ", cursor.getString(0));
}
cursor.moveToNext();
}
if (idsToDelete.endsWith(" OR ")) {
idsToDelete = idsToDelete.substring(0, idsToDelete.length()-4);
}
cr.delete(CalendarContract.Events.CONTENT_URI, idsToDelete, null);
I hope, it helps
You can use ContentProviderOperation with applyBatch to perform multiple deletes/inserts in a single transaction.
Check this SO answer for code sample.
after a long long research, I've got a sort of new doc of google calendar
you can add multiple args to a selection allowing you to find all desired rows and delete them all at once
val operationList = ArrayList<ContentProviderOperation>()
var contentProviderOperation: ContentProviderOperation
appointments.forEach {
contentProviderOperation = ContentProviderOperation
.newDelete(CalendarContract.Events.CONTENT_URI)
.withSelection(CalendarContract.Events.ORIGINAL_SYNC_ID + " =?", arrayOf(it.id))
.build()
operationList.add(contentProviderOperation)
}
contentResolver.applyBatch(CalendarContract.AUTHORITY, operationList)
I have added a new Calendar :
ContentValues event2= new ContentValues();
event.put("name", "My calendar");
event.put("displayName", "My new calendar");
event.put("hidden",0);
Uri url2 = getContentResolver().insert(calendarUri, event);
When I list all calendars , the new calendar appears but my native Calendar APP crash and now I can't the new calendar !I have searched here and I tested some ways :
Uri uri1=ContentUris.withAppendedId(calendarUri, calId);
int url3 = getContentResolver().delete(uri1,"_id="+calId",projection);
int url2 = getContentResolver().delete(calendarUri,"_id=3",projection);
But always shows the error: the name must no be empty :null ( the projection is a Array string with id and name)
Any idea?
Might be a bit late but :
Uri evuri = CalendarContract.Calendars.CONTENT_URI;
//substitue your calendar id into the 0
long calid = 0;
Uri deleteUri = ContentUris.withAppendedId(evuri, calid);
getActivity().getContentResolver().delete(deleteUri, null, null);
works for me.
Or if you don't know your calendar id :
Uri evuri = CalendarContract.Calendars.CONTENT_URI;
Cursor result = getActivity().getContentResolver().query(evuri, new String[] {CalendarContract.Calendars._ID, CalendarContract.Calendars.ACCOUNT_NAME, CalendarContract.Calendars.CALENDAR_DISPLAY_NAME}, null, null, null);
while (result.moveToNext())
{
if(result.getString(2).equals("YOUR CALENDAR NAME"))
{
long calid = result.getLong(0);
Uri deleteUri = ContentUris.withAppendedId(evuri, calid);
getActivity().getContentResolver().delete(deleteUri, null, null);
}
}
Of course, finding CalendarContract.Calendars.ACCOUNT_NAME may be redundant for you so just remove it. (there's barely any overhead really)
Try this:
getContentResolver.delete(ContentUris.withAppendedId(CalendarContract.Calendars.CONTENT_URI,
id), null, null);
Where id is the id of the calendar you'd like to delete.
Haven't tested it, but works for events.
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.
I'm trying to implement my first android Program. It should write calendar entries (I know, not the best task to begin programming Andorid).
I've tried:
Uri CALENDAR_URI = Uri.parse("content://calendar/events");
ContentResolver cr = getContentResolver();
cr.delete(CALENDAR_URI, null, null); // Delete all
cr.delete(CALENDAR_URI, "calendar_id=1", null); // Delete all in default calendar
cr.delete(CALENDAR_URI, "_id=1", null); // Delete specific entry
Nothing worked. I allays get a "cannot delete that URL".
Inserting an Calendar Entry was simple:
ContentValues values = new ContentValues();
values.put("calendar_id", 1);
values.put("title", this.title);
values.put("allDay", this.allDay);
values.put("dtstart", this.dtstart.toMillis(false));
values.put("dtend", this.dtend.toMillis(false));
values.put("description", this.description);
values.put("eventLocation", this.eventLocation);
values.put("visibility", this.visibility);
values.put("hasAlarm", this.hasAlarm);
cr.insert(CALENDAR_URI, values);
According to my insert method accessing the calendar worked.
Thanks, Arthur!
OK, one thing I didn't try:
Uri CALENDAR_URI = Uri.parse("content://calendar/events");
int id = 1; // calendar entry ID
Uri uri = ContentUris.withAppendedId(CALENDAR_URI, id);
cr.delete(uri, null, null);
This is what I was missing:
Uri uri = ContentUris.withAppendedId(CALENDAR_URI, id);
should lead to content://calendar/events/1
Now my Calendar is empty :-)
The right way to delete things out of a user's calendar is to use the appropriate GData APIs and delete it from their Google Calendar. Manipulating the Calendar application's content provider -- as you are trying to do -- is not part of the public API.