问题
The Android's developer documentation states that you can use a DriveApi.OnSyncFinishCallback to (presumably) handle when a synchronization between your local contet and your google drive account is completed. Normally such synchronization appens automatically, trasparently managed by Google Play Services, but apparently you can force a sync request with a call to:
Drive.DriveApi.requestSync(mGoogleApiClient);
I say "apparently" because the official documentation of this function 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))
Anyway, a OnSyncFinishCallback can be instantiated with 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 it will be called automatically when the sync is completed? The requestSync call returns a PendingResult that only have a setResultCallback(ResultCallback arg0) method, that can't be used for a OnSyncFinishCallback.
回答1:
I must say that requestSync is working absolutely fine for me (January 2015, with Google Play Services 6.5.87). I do a backup of my database on one device and restore it on another device, but before the restore I call requestSync this way:
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'm using the root folder, not the appfolder. The appfolder might have additional synchronization issues when installing/uninstalling the app from different devices, so for the moment I prefer to stick with root folder.
回答2:
OnSyncFinishCallback is a red herring, it shouldn't be exposed.
Just add a callback handler to requestSync like any other GoogleApiClient method:
Drive.Drive.requestSync(mGoogleApiClient).setResultCallback(
new ResultCallback<Success>() {
//...
});
回答3:
It turned out that OnSyncFinishCallback was removed from API and DriveAPI.requestSync() doesn't do what it's supposed to. Fortunately Google just introduced new Drive API for Android in version 6.1 of Google Play Services, in particular the Completion Events, that makes exactly what OnSyncFinishCallback was supposed to do. More official detail here https://developers.google.com/drive/android/completion
来源:https://stackoverflow.com/questions/23939356/how-to-use-the-driveapi-onsyncfinishcallback-androids-google-play-services