im trying to start the calendar sync programatically using this code
Bundle bundle = new Bundle();
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_FORCE, true);
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_SETTINGS, true);
ContentResolver.requestSync(accounts[0], "com.android.calendar", bundle);
i want a way so i can know when sync complete so i can read data from the calendar i tried doing this
while (ContentResolver.isSyncActive(accounts[0], "com.android.calendar")) {
System.out.println("looping: " + i);
}
readLocalCalendar();
readLocalEvents();
but the system exit the loop before the sync ends and i can still see the sync sign at the status bar, so any help so i can read calendar events after sync completle done ??
thanks
using the addStatusChangeListener actually worked for me .
here's a reference .
don't forget to add the needed permissions .
Another option would be to register a broadcast receiver to tell you when the sync is finished like this:
Inside your SyncAdapter use this when done:
You can also use this for when starting or updating the status of the sync ;)
Update: Updated the code to avoid leaks and making sure the activity is still active (onResume/onPause)
Use
ContentResolver.addStatusChangeListener (int mask, SyncStatusObserver callback)
to get notified of changes in sync status. docsPlease do not loop forever, its really bad design. Using the above method everything is asynchronous so you don't waste any cpu cycles.
You could also use
ContentResolver.registerContentObserver (Uri uri, boolean notifyForDescendents, ContentObserver observer)
docs to get notified in changes on a specific URI (like the calendar's URI)try an AsyncTask :
Good luck !