Trigger Android Calendar to sync after adding events programmatically

孤街醉人 提交于 2019-12-10 10:05:00

问题


I am using the "undocumented" API's to read and write calendar events, nothing too hard there. The problem though is after creating calendar events you have to wait for the polling period (I suppose) for the device to sync it's calendar, such as with gmail, for events to appear online. I have found that if you add an event to the calendar directly it will sync right away so something in the Calendar app is triggering an immediate synchronization.

Is there something I can do in code to notify the device to sync the calendar at completion of adding new events?

Thank you.


回答1:


The ContentResolver class has various helper methods to deal with syncing. Try ContentResolver.requestSync() method.




回答2:


public void syncCalendars()
    {
        Account[] accounts = AccountManager.get(context).getAccounts();
        Log.d(TAG, "Refreshing " + accounts.length + " accounts");
        String authority = CalendarContract.Calendars.CONTENT_URI.getAuthority();
        for (int i = 0; i < accounts.length; i++) {

            Log.d(TAG, "Refreshing calendars for: " + accounts[i]);

            Bundle extras = new Bundle();
            extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
            ContentResolver.requestSync(accounts[i], authority, extras);
        }
    }


来源:https://stackoverflow.com/questions/7094637/trigger-android-calendar-to-sync-after-adding-events-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!