无法创建同步Google日历Android

时间:2016-09-16 14:33:31

标签: android api calendar google-calendar-api

我正在尝试在Google帐户上创建日历,我设法创建了日历,但没有与谷歌同步,我不知道我做错了什么。

我知道问题或多或少,但我无法解决。

我使用的代码是:

public static long createCalendar (Activity activity, String name, String account, boolean local){

        String color = "blue";

        ContentValues calendarvalues = new ContentValues();

        //The account that was used to sync the entry to the device. If the account_type is not {@link #ACCOUNT_TYPE_LOCAL} then the name and
        // type must match an account on the device or the calendar will be deleted.
        if(local) {
            calendarvalues.put(CalendarContract.Calendars.ACCOUNT_NAME, "DUMMYLOCAL");
            calendarvalues.put(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
        }else{
            calendarvalues.put(CalendarContract.Calendars.ACCOUNT_NAME, account);
            calendarvalues.put(CalendarContract.Calendars.ACCOUNT_TYPE, account);
        }
        //Local  CalendarContract.ACCOUNT_TYPE_LOCAL

        calendarvalues.put(CalendarContract.Calendars.NAME, name);
        calendarvalues.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, name);
        calendarvalues.put(CalendarContract.Calendars.CALENDAR_COLOR, Color.parseColor(color));
        calendarvalues.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_OWNER);
//        //None          CalendarContract.Calendars.CAL_ACCESS_NONE          Cannot access the calendar
//        //freeBusy      CalendarContract.Calendars.CAL_ACCESS_FREEBUSY      Can only see free/busy information about the calendar
//        //Read          CalendarContract.Calendars.CAL_ACCESS_READ          Can read all event details
//        //Respond       CalendarContract.Calendars.CAL_ACCESS_RESPOND       Can reply yes/no/maybe to an event
//        //Override      CalendarContract.Calendars.CAL_ACCESS_OVERRIDE      not used
//        //Contributor   CalendarContract.Calendars.CAL_ACCESS_CONTRIBUTOR   Full access to modify the calendar, but not the access control settings
//        //Editor        CalendarContract.Calendars.CAL_ACCESS_EDITOR        Full access to modify the calendar, but not the access control settings
//        //Owner         CalendarContract.Calendars.CAL_ACCESS_OWNER         Full access to the calendar
//        //Root          CalendarContract.Calendars.CAL_ACCESS_ROOT          Domain admin

        calendarvalues.put(CalendarContract.Calendars.OWNER_ACCOUNT, account);
        calendarvalues.put(CalendarContract.Calendars.VISIBLE, 1);
        calendarvalues.put(CalendarContract.Calendars.SYNC_EVENTS, 1);

//        calendarvalues.put(CalendarContract.Calendars.CALENDAR_LOCATION, "Spain");


        Uri calUri = null;
        Uri result = null;

        if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {

            PermissionUtil.requestCalendarPermission(activity);

            return -1;
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            calUri = CalendarContract.Calendars.CONTENT_URI;
        }else{
            calUri = Uri.parse("content://com.android.calendar/calendars");
        }

        if(calUri != null) {
            if(local) {
                calUri = calUri.buildUpon()
                        .appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
                        .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, "DUMMYLOCAL")
                        .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL)
                        .build();
            }else {
                calUri = calUri.buildUpon()
                        .appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
                        .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, account)
                        .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, account)
                        .build();
            }

            result = activity.getContentResolver().insert(calUri, calendarvalues);
        }

        if (result != null) {
            try {
                return Long.parseLong(result.getLastPathSegment());
            } catch (Exception e) {
                return -1;
            }
        }
        return -1;
    }

我认为错误在于这一行:

calendarvalues.put(CalendarContract.Calendars.ACCOUNT_TYPE, account);

因为我看到了以下查询返回的值:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
     calUri = CalendarContract.Calendars.CONTENT_URI;
}else{
     calUri = Uri.parse(calendarUriString);
}

String[] projection = new String[]{
            CalendarContract.Calendars._ID,
            CalendarContract.Calendars.NAME,
            CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
            CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL,
            CalendarContract.Calendars.ACCOUNT_NAME,
            CalendarContract.Calendars.ACCOUNT_TYPE,
    //      CalendarContract.Calendars.CALENDAR_COLOR,
            CalendarContract.Calendars.OWNER_ACCOUNT,
            CalendarContract.Calendars.VISIBLE,
            CalendarContract.Calendars.SYNC_EVENTS,
 };

 Cursor cursor = activity.getContentResolver().query(calUri, projection, null, null, null);

结果如下:

id:1
姓名:我的日历@本地
显示名称:我的日历
访问级别:700
AccountName:我的日历@本地
AccountType:com.local
ownerAccount:所有者帐户可见:1
同步:1

id:2
姓名:test@gmail.com
显示名称:test@gmail.com
访问级别:700
AccountName:test@gmail.com
AccountType:com.google
ownerAccount:test@gmail.com
可见:1
同步:1

id:3
名称:测试校准​​
显示名称:测试校准​​
访问级别:700
AccountName:test@gmail.com
AccountType:test@gmail.com
ownerAccount:1
可见:1
同步:1

我试着提出以下内容:

calendarvalues.put(CalendarContract.Calendars.ACCOUNT_TYPE, "com.google");

但未创建日历。

如果有人知道该怎么做或有任何可能有用的示例或文档,那将会有所帮助。

提前致谢。

1 个答案:

答案 0 :(得分:1)

我正在努力解决同样的问题 - 我最终使用Google Calendar API insert功能创建了一个日历。使用Quickstart Guide,您应该可以创建日历。

像这样使用MakeRequestTask#mService

Calendar newCalendar = new Calendar();
newCalendar.setSummary("Calendar Name");
newCalendar.setTimeZone(TIME_ZONE); //assuming you have it as a constant somewhere
String newCalendarId = null;
try {
        com.google.api.services.calendar.model.Calendar insertedCalendar = mService.calendars().insert(newCalendar).execute();
        newCalendarId = insertedCalendar.getId(); 
} catch(Exception ignore){}

然后强制与

同步帐户
Bundle extras = new Bundle();
extras.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
ContentResolver.requestSync(selectedAccount, CalendarContract.AUTHORITY, extras);