4

我正在尝试使用以下代码在 android 中添加我自己的日历。我也在清单中提供了读写权限。

我的错误是:“创建日历时出错,名称不能为空:null”虽然我已经提供了名称...

任何人请帮我解决它,并提供在 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);
4

2 回答 2

2
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;
        }
    }
于 2013-01-11T09:09:17.420 回答
0

你应该尝试这样的事情:

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);
于 2012-12-17T02:00:22.887 回答