Create new synced calendar with android api
android, api, calendar
Solution
Not possible for now.
After a lot of googling I could not find a way to create new syncable calendar in Gmail account (account type "com.google"). I've tried these scenarios: 1. without SyncAdapter and I get the same behaviour - calendar appears in external Google Calendar app with all events I added but shortly after (e.g. after couple of seconds) all events and calendar disappear. 2. with SyncAdapter and com.google authenticator - I get an error during the process 3. with SyncAdapter and com.example authenticator - possible to create calendar but not in Gmail account
Note you can create new account and create new calendar for that account (using SyncAdapter, scenario 3) but it's not a Gmail account so calendar is not syncable with Gmail Calendar (e.g. if you login to Gmail account in web browser and open Google Calendar that calendar you've just created will not show).
Problem
i'm trying to create a calendar on my account to fill with events that i get from some websites. I've searched and found some new android 4.0 calendar example that i've modified to obtain what i need. The problem is that the calendar is created, filled with events but not synced with google calendar, so in the next sync it is erased. The funcion i use are these: This is the one for add the new calendar if don't alreay exist: ``` public static Uri createCalendarWithName(Context ctx, String name,String accountName) { Uri target = Uri.parse(CalendarContract.Calendars.CONTENT_URI.toString()); target = target.buildUpon().appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true") .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, accountName) .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, "com.google").build(); ContentValues values = new ContentValues(); values.put(Calendars.ACCOUNT_NAME, accountName); values.put(Calendars.ACCOUNT_TYPE, "com.google"); values.put(Calendars.NAME, name); values.put(Calendars.CALENDAR_DISPLAY_NAME, name); values.put(Calendars.CALENDAR_COLOR, 0x00FF00); values.put(Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_ROOT); values.put(Calendars.OWNER_ACCOUNT, accountName); values.put(Calendars.VISIBLE, 1); values.put(Calendars.SYNC_EVENTS, 1); values.put(Calendars.CALENDAR_TIME_ZONE, "Europe/Rome"); values.put(Calendars.CAN_PARTIALLY_UPDATE, 1); values.put(Calendars.CAL_SYNC1, "https://www.google.com/calendar/feeds/" + accountName + "/private/full"); values.put(Calendars.CAL_SYNC2, "https://www.google.com/calendar/feeds/default/allcalendars/full/" + accountName); values.put(Calendars.CAL_SYNC3, "https://www.google.com/calendar/feeds/default/allcalendars/full/" + accountName); values.put(Calendars.CAL_SYNC4, 1); values.put(Calendars.CAL_SYNC5, 0); values.put(Calendars.CAL_SYNC8, System.currentTimeMillis()); Uri newCalendar = ctx.getContentResolver().insert(target, values); return newCalendar; } ``` and that one create the new event without interaction: ``` public static Uri createEventWithName(Context ctx, long id, String name, String data) { long startMillis = 0; long endMillis = 0; int id2=(int)id; String[] divisi = data.split("/"); Calendar beginTime = Calendar.getInstance(); beginTime.set(2012,Integer.parseInt(divisi[0])-1, Integer.parseInt(divisi[1])); startMillis = beginTime.getTimeInMillis(); Calendar endTime = Calendar.getInstance(); endTime.set(2012,Integer.parseInt(divisi[0])-1, Integer.parseInt(divisi[1])); endMillis = endTime.getTimeInMillis(); ContentValues cv = new ContentValues(); cv.put(Events.TITLE, name); cv.put(Events.DTSTART, startMillis); cv.put(Events.DTEND, endMillis); cv.put(Events.CALENDAR_ID, id2); Log.d("aggiungo a calendario",Integer.toString(id2)); cv.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().toString()); //cv.put(Events.RRULE, "FREQ=DAILY;INTERVAL=2"); Uri newEvent = ctx.getContentResolver().insert(CalendarContract.Events.CONTENT_URI, cv); return newEvent; } ``` I'm not so experienced in Android programming so i think it's a stupid question =) i've read that accountName and Account Type must be the same that the one stored on android device, else the event is cancelled. I get accountName from android api and i think they are correct. The account type seems to work for other.... Thanks to anybody that help me!