Calendar "sync now" from app

cmweb

Active Member
Licensed User
Longtime User
Hi,

I would like to trigger a Google calendar sync from my app.

As it would happen when I select "sync now" at the Google account settings.

Is this possible?

Best regards,

Carsten
 

pesquera

Active Member
Licensed User
Longtime User
thank you for your help so much, but.. I have not idea about how to implement that with B4A.. is there some tutorial?
 
Upvote 0

pesquera

Active Member
Licensed User
Longtime User
Ok. sorry.. I'm already adding dates from my App. Now, I would like to trigger a Google calendar sync from my App (like cmweb on the first post). The problem is the delay I have after editing a date on my Calendar (App) so, I want to do it by event. Updating dates on my Gmail account (via web) has not this delay, I can see the changes on my App instantly.. why?
 
Upvote 0

eps

Expert
Licensed User
Longtime User
Ah I think I understand now. I'm not sure how you do that. Will have to dig a little in to the API and see what's there...
 
Upvote 0

pesquera

Active Member
Licensed User
Longtime User
thank you for the link.. I have no experience on java, just B4A.. how can I translate this?

Sample code
The snippet of sample code below demonstrates how to use sync tokens with the Java client library. The first time the run method is called it will perform a full sync and store the sync token. On each subsequent execution it will load the saved sync token and perform an incremental sync.

B4X:
  private static void run() throws IOException {
    // Construct the {@link Calendar.Events.List} request, but don't execute it yet.
    Calendar.Events.List request = client.events().list("primary");

    // Load the sync token stored from the last execution, if any.
    String syncToken = syncSettingsDataStore.get(SYNC_TOKEN_KEY);
    if (syncToken == null) {
      System.out.println("Performing full sync.");

      // Set the filters you want to use during the full sync. Sync tokens aren't compatible with
      // most filters, but you may want to limit your full sync to only a certain date range.
      // In this example we are only syncing events up to a year old.
      Date oneYearAgo = Utils.getRelativeDate(java.util.Calendar.YEAR, -1);
      request.setTimeMin(new DateTime(oneYearAgo, TimeZone.getTimeZone("UTC")));
    } else {
      System.out.println("Performing incremental sync.");
      request.setSyncToken(syncToken);
    }

    // Retrieve the events, one page at a time.
    String pageToken = null;
    Events events = null;
    do {
      request.setPageToken(pageToken);

      try {
        events = request.execute();
      } catch (GoogleJsonResponseException e) {
        if (e.getStatusCode() == 410) {
          // A 410 status code, "Gone", indicates that the sync token is invalid.
          System.out.println("Invalid sync token, clearing event store and re-syncing.");
          syncSettingsDataStore.delete(SYNC_TOKEN_KEY);
          eventDataStore.clear();
          run();
        } else {
          throw e;
        }
      }

      List<Event> items = events.getItems();
      if (items.size() == 0) {
        System.out.println("No new events to sync.");
      } else {
        for (Event event : items) {
          syncEvent(event);
        }
      }

      pageToken = events.getNextPageToken();
    } while (pageToken != null);

    // Store the sync token from the last request to be used during the next execution.
    syncSettingsDataStore.set(SYNC_TOKEN_KEY, events.getNextSyncToken());

    System.out.println("Sync complete.");
  }
 
Last edited:
Upvote 0

davfla

Member
Licensed User
Longtime User
I had the same problem and found a solution. I needed a few hours for this short code, but it works. The sync have a little bit delay for 3-10 seconds, but it is more faster as without than.

Try
HTML:
Dim jo, jo1 As JavaObject
Dim cls As JavaObject = jo.InitializeStatic("android.content.ContentResolver")
Dim context As JavaObject = jo1.InitializeNewInstance("android.os.Bundle", Null)
cls.RunMethod("requestSync", Array As Object(Null, Null, context))

Run this code after every change.
 
Upvote 0
Top