create Calendar With Name - Android Android OS

Android examples for Android OS:Calendar Name

Description

create Calendar With Name

Demo Code


//package com.java2s;

import android.accounts.Account;
import android.accounts.AccountManager;

import android.content.ContentValues;
import android.content.Context;

import android.net.Uri;
import android.provider.CalendarContract;

import android.provider.CalendarContract.Calendars;

public class Main {
    public static Uri createCalendarWithName(Context ctx, String name) {
        Account[] accounts = AccountManager.get(ctx).getAccountsByType(
                "com.google");
        if (accounts == null || accounts.length < 1) {
            throw new RuntimeException(
                    "No google account configured on this device!");
        }// w  w w  .j a  v a2s . c  o m
        //prendiamo soltanto il primo degli account configurati per semplicita
        String accountName = accounts[0].name;

        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");
        //inseriamo lo stesso valore nome e display name per semplicita
        values.put(Calendars.NAME, name);
        values.put(Calendars.CALENDAR_DISPLAY_NAME, name);

        values.put(Calendars.CALENDAR_COLOR, 0xFF0000);
        values.put(Calendars.CALENDAR_ACCESS_LEVEL,
                CalendarContract.Calendars.CAL_ACCESS_OWNER);
        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/emanuele.disaverio%40gmail.com/private/full");
        values.put(
                Calendars.CAL_SYNC2,
                "https://www.google.com/calendar/feeds/default/allcalendars/full/emanuele.disaverio%40gmail.com");
        values.put(
                Calendars.CAL_SYNC3,
                "https://www.google.com/calendar/feeds/default/allcalendars/full/emanuele.disaverio%40gmail.com");
        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;
    }
}

Related Tutorials