Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Adding a new Calendar

I've checked all over for ways to create a new calendar from within an Android app. The only way I've seen is using the new Calendar API in the latest api version, BUT this only seems to work if you use CalendarContract.ACCOUNT_TYPE_LOCAL. It won't let you create a Calendar attached to the users Google account, synchronized with Google Calendar. It doesn't appear in the calendar list online at Google Calendar.

values.put(CalendarContract.Calendars.ACCOUNT_TYPE, 
    CalendarContract.ACCOUNT_TYPE_LOCAL);

I've tried using the account type from the Account, but this does not work. It doesn't seem to produce any error though. The result comes back fine.

values.put(CalendarContract.Calendars.ACCOUNT_TYPE, accountType);

Here is the full code for adding a new local calendar using the Calendar API and SYNCADAPTER (account type commented out). Does anyone know if what I'm trying to do is even possible?

AccountManager manager = AccountManager.get(context);
Account[] accounts = manager.getAccountsByType("com.google");

String accountName = "";
String accountType = "";

for (Account account : accounts) {
    accountName = account.name;
    accountType = account.type;
    break;
}

ContentResolver cr = context.getContentResolver();
ContentValues values = new ContentValues();

values.put(CalendarContract.Calendars.ACCOUNT_NAME, accountName);
//values.put(CalendarContract.Calendars.ACCOUNT_TYPE, accountType);
values.put(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
values.put(CalendarContract.Calendars.NAME, DaysSince.CAL_NAME);
values.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, DaysSince.CAL_NAME);
values.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
values.put(CalendarContract.Calendars.VISIBLE, 1);
values.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_OWNER);
values.put(CalendarContract.Calendars.OWNER_ACCOUNT, accountName);
values.put(CalendarContract.Calendars.DIRTY, 1);
values.put(CalendarContract.Calendars.CALENDAR_TIME_ZONE, TimeZone.getDefault().getID());

Uri calUri = CalendarContract.Calendars.CONTENT_URI;

calUri = calUri.buildUpon()
        .appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
        .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, accountName)
        .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, accountType)
        .build();

Uri result = cr.insert(calUri, values);
like image 428
Ben Gardner Avatar asked Sep 24 '12 14:09

Ben Gardner


People also ask

Can I have 2 separate Google calendars on my phone?

Not only can you manage multiple calendars under one account, you can manage them from multiple accounts. Open the app, tap the hamburger icon, and browse through the list of calendars under each of your Google accounts.


1 Answers

Yes you can do it. But since you want to create a calendar linked with your google account, you need Google Calendar API for this (didn't find a single solution for doing this with Android Calendar Provider). Check below links:

  • Google calendar API for Android

  • Insert new Google Calendar

Be sure to check their API explorer. Simple HTTP REQUEST for creating new calendar:

POST https://www.googleapis.com/calendar/v3/calendars?key={YOUR_API_KEY}

{
 "summary": "My new google calendar"
}

And it's RESPONSE:

{
 "kind": "calendar#calendar",
 "etag": "__________etag_val_________",
 "id": "[email protected]",
 "summary": "My new google calendar"
}

After that you should be able to manage this calendar from your Android app with Calendar Provider (after sync).

like image 139
Luke Avatar answered Sep 22 '22 13:09

Luke