Search code examples
androidandroid-activityandroid-calendarandroid-event

Get events added by my app in android calendar


In my app, I am adding some event in the google calendar on a screen. When the user comes to the screen again, it adds the events again so in calendar the duplicate event is also visible. So before adding any new event, I want to check if the event already exists. If I get all the events of the calendar and iterate over it, my app becomes very slow or hangs with a notification

too many calendar deletes

The code that I am using to insert the values is :

final ContentValues event = new ContentValues();


    event.put(CalendarContract.Events.CALENDAR_ID, calendarId);

    event.put(CalendarContract.Events.TITLE, name);
    event.put(CalendarContract.Events.DTSTART, millisstarttime);
    event.put(CalendarContract.Events.DTEND, millisendtime);
    event.put(CalendarContract.Events.EVENT_LOCATION, venue);
    event.put(CalendarContract.Events.ALL_DAY, 0);   // 0 for false, 1 for true
    event.put(CalendarContract.Events.HAS_ALARM, 1);
    event.put(CalendarContract.Events.ACCESS_LEVEL, CalendarContract.Events.ACCESS_PRIVATE);
    event.put(CalendarContract.Events.AVAILABILITY, CalendarContract.Events.AVAILABILITY_BUSY);// 0 for false, 1 for true

    String timeZone = TimeZone.getDefault().getID();
    event.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone);

    this.getContentResolver().insert(baseUri, event);

And I am trying to get the values :

private void deleteeve(ContentResolver cr,int calendarId) {

    String[] projection = {CalendarContract.Events._ID,
            CalendarContract.Events.CUSTOM_APP_PACKAGE,CalendarContract.Events.CALENDAR_ID,CalendarContract.Events.ORGANIZER};
    // Get a Cursor over the Events Provider.
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        return;
    }
    Cursor cursor = getContentResolver().query(
            CalendarContract.Events.CONTENT_URI, projection,"calendar_id=" + calendarId, null,
            null);
    assert cursor != null;
    cursor.moveToFirst();

    String idsToDelete = "";
    int nameIdx = cursor.getColumnIndexOrThrow(CalendarContract.Events.CUSTOM_APP_PACKAGE);
    int id=cursor.getColumnIndexOrThrow(CalendarContract.Events._ID);
    int org=cursor.getColumnIndexOrThrow(CalendarContract.Events.ORGANIZER);
    while (cursor.moveToNext()) {
        // Extract the name.
        String name = cursor.getString(nameIdx);
        String apppackage = getApplicationContext().getPackageName();
        String organizer=cursor.getString(org);
        String idevent = cursor.getString(id);
        for (int i = 0; i < cursor.getCount(); i++) {

            if (apppackage.equals(name)) {
                // MY CODE
            }

        }
    }

}

I tried getting the events using package name, So is there anyway to get the events added by my app only, other than iterating over all events?


Solution

  • I fixed it myself. I was trying everything correct. It was hanging because it was trying to delete 200 events on the main thread. Only thing I did was started the deletion and the saving of events in a sepatate Thread.

    Thread mThread = new Thread(new Runnable(){
     @override
     public void run(){
        //MY stuff
     }
    
    });
    

    THat's all.