How to use DriveApi.OnSyncFinishCallback (Android Google Play Services)

The Android developer documentation states that you can use DriveApi.OnSyncFinishCallback for the (presumably) handle when synchronization between your local account and your Google account is complete. Typically, this synchronization is automatically added, managed by Google Play Services, but apparently you can force a synchronization request with a call:

Drive.DriveApi.requestSync(mGoogleApiClient); 

I say β€œapparently” because the official documentation for this feature is very poor, at least ( https://developer.android.com/reference/com/google/android/gms/drive/DriveApi.html#requestSync ( com.google.android.gms.common.api.GoogleApiClient) )

In any case, OnSyncFinishCallback can be created using this code:

 OnSyncFinishCallback myCallback = new OnSyncFinishCallback(){ @Override public void onSyncFinish(com.google.android.gms.common.api.Status arg0) { // TODO Auto-generated method stub } }; 

My question is where and how can I register this callback so that it is automatically called when synchronization is complete? The requestSync request returns PendingResult, which has only the setResultCallback (ResultCallback arg0) method, which cannot be used for OnSyncFinishCallback.

+2
source share
3 answers

It turned out that OnSyncFinishCallback was removed from the API, and DriveAPI.requestSync () did not do what it should. Fortunately, Google just introduced the new Android API in version 6.1 of the Google Play services, in particular, the completion events, which does exactly what OnSyncFinishCallback should have done. More official information here https://developers.google.com/drive/android/completion

+2
source

I have to say that requestSync works absolutely fine for me (January 2015, with Google Play Services 6.5.87). I backup my database on one device and restore it on another device, but before recovery I call requestSync as follows:

  Drive.DriveApi.requestSync(mGoogleApiClient) .setResultCallback(new ResultCallback<Status>() { @Override public void onResult(Status result) { if (!result.isSuccess()) { // Sync not ok showMessage("Sync error"); return; } // Sync ok. I can safely do a query to get // the database file and restore it. ... 

By the way, I use the root folder, not the application folder. When installing / uninstalling the application from different devices, the application may have additional problems with synchronization, so at the moment I prefer to stick to the root folder.

+3
source

OnSyncFinishCallback is a red herring, it cannot be opened.

Just add a callback handler for the Sync request, like any other GoogleApiClient method:

 Drive.Drive.requestSync(mGoogleApiClient).setResultCallback( new ResultCallback<Success>() { //... }); 
+2
source

Source: https://habr.com/ru/post/1210894/


All Articles