2016-09-16 65 views
1

我想創建日曆在谷歌帳戶,我設法創建日曆但沒有與谷歌同步,我不知道我在做什麼錯了。無法創建同步谷歌日曆Android

我知道或多或少的問題,但我無法修復它。

我使用的代碼是這樣的:

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
帳戶名:我的日曆@本地
ACCOUNTTYPE:com.local
ownerAccount:所有者帳戶可見:1
同步:1

ID:2
名稱:[email protected]
顯示名稱:[email protected]
訪問級別:700
帳戶名:[email protected]
ACCOUNTTYPE:com.google
ownerAccount:[email protected]
可見:1
同步:1

ID:3
名稱:測試校準​​
顯示名稱:測試校準​​
訪問級別:700
帳戶名:[email protected]
ACCOUNTTYPE:[email protected]
ownerAccount:1
可見:1
同步:1

我試圖把以下內容:

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

,但不會創建日曆。

如果有人知道如何做或有任何可能有用的示例或文檔,這將有所幫助。

在此先感謝。

+0

帳戶同步「我設法創造的日曆,但沒有與谷歌同步」,究竟是你想怎麼辦? – noogui

+0

@noogui我正在嘗試創建一個與Google帳戶同步但不是本地的日曆,並且稍後會創建事件,並且這將與不同設備上看到它的Google帳戶同步,而無需爲每個設備創建。對於沒有任何意圖選擇選項的用戶也必須是透明的。 – Joacer

回答

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); 
+0

謝謝你的回答!我會嘗試他們 – Joacer