我在Android N開始的完整日曆應用中遇到了一個奇怪的問題。我想知道如果我錯過了某些內容,或者Android N上的日曆提供程序出現問題。無法在Android N中創建日曆事件
我正在使用日曆內容提供程序來管理日曆。可以打開,編輯,刪除和創建事件(從Android 2.1到棉花糖),但現在在N上無法創建新事件。當試圖創建一個新的事件時,事件暫時出現在事件列表中,然後消失。當在日誌中尋找,這到底發生了什麼:
EventHandler::InsertEvent: Forbidden Entity[ id=2172 calendar_id=1 calendar_sync_id=-899202677 ]
com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden {
"errors": [
{
"reason": "forbidden",
"domain": "global",
"message": "Forbidden"
}],
"message": "Forbidden",
"code": 403
}
所有我可以用這個錯誤與日曆API調用,而不是與內容提供商相關的發現(我的猜測是,在內部使用日曆API和有些東西因缺乏認證而失敗?)。 但是,我查看了提供程序文檔https://developer.android.com/guide/topics/providers/calendar-provider.html,找不到任何身份驗證的要求。
我查找了AOSP當前日曆應用程序的更改,但找不到任何相關內容。它們作爲同步適配器運行,使您可以在我不使用的特殊列上編寫代碼,因此我不認爲這是問題的原因。
我也在考慮使用Calendar API,但這不會被期望,因爲不會脫機工作,並且日曆API文檔本身指出:「注意:此快速入門的目的是演示如何使用在日曆API的Android應用程序,但生產日曆應用程序將通過使用日曆提供程序內容提供商,脫機工作,收到推動,是一個很好的匹配平臺強烈受益。「
我一直在很長時間沒有運氣,玩Google Play客戶端/服務庫,嘗試使用Google登錄,清單權限和幾個樣本,畢竟我不知道現在要去哪裏,我真的很感激你們中的任何人都可以讓我知道什麼是w榮或什麼在Android N.寫的日曆事件時
非常感謝你去預期的方式
下面的代碼:
public Uri AddCalendarEntry(CalendarEventObject eventObject) {
ContentValues event = new ContentValues();
if (eventObject.getCalendar() != null) {
event.put(CalendarContract.Events.CALENDAR_ID, Integer.parseInt(eventObject.getCalendar().getId()));
} else {
Log.e (DEBUG_TAG,"calendar id not available ");
return null;
}
event.put(CalendarContract.Events.TITLE, eventObject.getSubject());
event.put(CalendarContract.Events.DESCRIPTION, eventObject.getDescription());
event.put(CalendarContract.Events.EVENT_LOCATION, eventObject.getLocation());
event.put(CalendarContract.Events.STATUS, CalendarContract.Events.STATUS_CONFIRMED);
event.put(CalendarContract.Events.HAS_ALARM, 1); // 0 for false, 1 for true
// All day events must use timezone UTC, others can use default
if (eventObject.isAllDay()) {
event.put(CalendarContract.Events.ALL_DAY, 1); // 1 for true
event.put(CalendarContract.Events.EVENT_TIMEZONE, Time.TIMEZONE_UTC);
} else {
event.put(CalendarContract.Events.ALL_DAY, 0); // 0 for false
event.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().toString());
}
// Recurrent events must use "duration", normal events should use "dtend"
event.put(CalendarContract.Events.DTSTART, eventObject.getStartDateTime());
String recurrence = eventObject.getRecurrence();
if (recurrence != null && !TextUtils.isEmpty(recurrence)) {
String duration = datesToDuration(eventObject.getStartDateTime(), eventObject.getEndDateTime());
event.put(CalendarContract.Events.DURATION, duration);
event.put(CalendarContract.Events.RRULE, recurrence);
} else {
event.put(CalendarContract.Events.DTEND, eventObject.getEndDateTime());
}
Uri eventsUri = CalendarContract.Events.CONTENT_URI;
Uri insertedUri = null;
try {
insertedUri = mainActivity.getContentResolver().insert(eventsUri, event);
} catch(Exception e) {
Log.e(DEBUG_TAG, "Error creating event " + e);
}
return insertedUri;
}
這裏全部跟蹤:
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
at
com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1065)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
at com.google.android.syncadapters.calendar.CalendarRequestExecutor.executeInternal(CalendarRequestExecutor.java:146)
at com.google.android.syncadapters.calendar.CalendarRequestExecutor.execute(CalendarRequestExecutor.java:120)
at com.google.android.syncadapters.calendar.EventHandler.sendEntityToServer(EventHandler.java:485)
at com.google.android.syncadapters.calendar.CalendarSyncAdapterApiary.sendEntityToServer(CalendarSyncAdapterApiary.java:4084)
at com.google.android.syncadapters.calendar.CalendarSyncAdapterApiary.processLocalChanges(CalendarSyncAdapterApiary.java:4031)
at com.google.android.syncadapters.calendar.CalendarSyncAdapterApiary.processLocalChangesForHandler(CalendarSyncAdapterApiary.java:3970)
at com.google.android.syncadapters.calendar.CalendarSyncAdapterApiary.performUpsync(CalendarSyncAdapterApiary.java:791)
at com.google.android.syncadapters.calendar.CalendarSyncAdapterApiary.performSync(CalendarSyncAdapterApiary.java:691)
at com.google.android.syncadapters.calendar.CalendarSyncAdapterApiary.onPerformLoggedSync(CalendarSyncAdapterApiary.java:504)
at com.android.emailcommon.syncadapter.LoggingThreadedSyncAdapter.onPerformSync(LoggingThreadedSyncAdapter.java:50)
at android.content.AbstractThreadedSyncAdapter$SyncThread.run(AbstractThreadedSyncAdapter.java:272)