Sachin Trivedi
Sachin Trivedi

Reputation: 2053

android calendar creation error

I am trying to add my own calendar in android using the following code. I have provided read write permission also in manifest.

and my error is : "Error while creating calendar the name must not be empty: null" though i have provided the name...

any one please hep me to solve it and also provide link to working solution of creating calendar in android.

                    final Uri calUri = Uri.parse("content://com.android.calendar/calendars");

                    android.accounts.Account account;
                    ContentValues vals = new ContentValues();
                    vals.put("_id", 1);

                    vals.put("_sync_account_type", "ACCOUNT_TYPE_LOCAL");
                    vals.put("name","sachin" );
                    vals.put("displayName","my_Cal");
                    vals.put("color", 14417920);
                    vals.put("access_level", 700);
                    vals.put("selected", 1);

                    vals.put("sync_events", 1);
                    vals.put("timezone", "GMT");
                    vals.put("hidden", 0);
                    Uri result=  getContentResolver().insert(calUri, vals);


                    System.out.println(result);

Upvotes: 4

Views: 653

Answers (2)

Sachin Trivedi
Sachin Trivedi

Reputation: 2053

public String createCalendar(String AccountName, String CalendarName,String Color) 

{
        try {

            Uri target = Uri.parse("content://com.android.calendar/calendars");
            target = target.buildUpon()
                    .appendQueryParameter("caller_is_syncadapter", "true")
                    .appendQueryParameter("account_name", AccountName)
                    .appendQueryParameter("account_type", "com.google").build();

            // String calUriString = "content://calendar/calendars";
            ContentValues values = new ContentValues();
            values.put("name", AccountName);
            values.put("account_name", AccountName);
            values.put("account_type", "com.google");
            values.put("calendar_displayName", CalendarName);
            values.put("calendar_color", Color);
            values.put("calendar_access_level", "700");

            Uri calendarUri = getContentResolver().insert(target, values);
            Log.v("calendar Uri", calendarUri.toString());
            String newCalID = calendarUri.toString().substring(
                    calendarUri.toString().lastIndexOf("/") + 1,
                    calendarUri.toString().indexOf("?"));
            Log.v("calendar Id", newCalID);
            return newCalID;

        } catch (Exception e) {
            System.out.println(e.toString());
            return null;
        }
    }

Upvotes: 2

lokoko
lokoko

Reputation: 5803

You should try something like this :

vals.put(Calendar.Calendars._SYNC_ACCOUNT_TYPE, "ACCOUNT_TYPE_LOCAL");
vals.put(Calendar.Calendars.NAME,"sachin");
vals.put(Calendar.Calendars.DISPLAY_NAME,"my_Cal");
vals.put(Calendar.Calendars.COLOR, 14417920);
vals.put(Calendar.Calendars.ACCESS_LEVEL, 700);
vals.put(Calendar.Calendars.SELECTED, 1);
vals.put(Calendar.Calendars.SYNC_EVENTS, 1);
vals.put((Calendar.Calendars.TIMEZONE, "GMT");
vals.put(Calendar.Calendars.HIDDEN, 0);

Upvotes: 0

Related Questions