2016-05-25 19 views
0

我使用具有READ_CALENDAR權限的Google Calendar API從日曆中查詢某些足球比賽,並將它們與我的裁判應用程序的結果進行集成顯示。目前我查詢日曆onConnected,如果我在用戶的日曆中添加新遊戲,那麼這往往不夠。有沒有辦法從Google Calendar設置推送通知到我的android應用程序?

很顯然,當我顯示數據時,我可以重新查看日曆API,但我希望能夠聽到來自Calendar API的推送通知,說明已添加新事件。這裏的API:https://developer.android.com/guide/topics/providers/calendar-provider.html沒有提及任何東西。

但是,這裏:https://developers.google.com/google-apps/calendar/v3/push談到推送通知到您自己的服務器。有沒有人知道一種方式來從應用程序做等價物?

從答案更新。這段代碼看起來是否合理,特別是在泄漏資源方面?

private void updateGamesFromCalendar() { 
    //get Calendar games if allowed 
    if (checkCalendar()) { 
     //Look for events with the keyword list in the Title, and dates between today-7 and today+7 
     long nowMs = System.currentTimeMillis(); 
     String[] eventSelectionArgs = new String[]{Long.toString(nowMs - TimeUnit.DAYS.toMillis(7)),Long.toString(nowMs + TimeUnit.DAYS.toMillis(7)) }; 
     if ((mEventsCursor != null) && (mCalendarObserver != null)) mEventsCursor.unregisterContentObserver(mCalendarObserver); 
     mEventsCursor = getContentResolver().query(RefWatchSettings.uriEvents, RefWatchSettings.EVENT_PROJECTION, RefWatchSettings.whereClauseEvents, eventSelectionArgs, RefWatchSettings.orderByEvents); 
     if (mCalendarObserver == null) mCalendarObserver = new CalendarEventObserver(null); 
     mEventsCursor.registerContentObserver(mCalendarObserver); 

     //delete future Calendar games and replace with the new query 
     Game.removeFutureCalendarGames(mGames); 
     removeFutureCalendarGamesFromDataItem(); 

     getGamesFromCalendar(); 
     refreshGameFragments(); 
     sendFutureGameNotifications(); 
    } 
} 

回答

1

使用查詢檢索日曆事件(請參閱Google Doc)。查詢返回Cursor對象(請參閱Cursor JavaDoc)。而Cursor對象可以用registerContentObserver()觀察。 因此,對日曆事件進行查詢,註冊一個Content Observer並讓你的Cursor打開。你應該通過你的內容觀察者接收事件的更新。

+0

感謝您的快速響應!我會去看看。 – Opus1217

+0

謝謝,這工作得很好。我想檢查一下我是否正確地進行了此操作,並且不會泄漏 - 對我上面的實施有任何意見? – Opus1217

相關問題