Hide
Drive API for Android

Listening for Change Events

You can use change listeners to receive notifications whenever a specified file or folder has changes to its contents or metadata. A change listener implements the ChangeListener interface for the ChangeEvent and receives a direct callback from the Drive service to a currently connected client application.

You can add a change listener by calling the addChangeListener method of the file or folder you want to receive notifications for. Listeners are only active for the durations of the current connection or until the removeChangeListener method is called on the same file or folder with the same callback parameter.

The following example code shows the logic you might implement to add a change listener to a file:

DriveFile file = Drive.DriveApi.getFile(mGoogleApiClient,
        mSelectedFileId);
file.addChangeListener(mGoogleApiClient, changeListener);

Conversely, to remove a change listener from a file, you can use code similar to the following example:

DriveFile file = Drive.DriveApi.getFile(mGoogleApiClient,
        mSelectedFileId);
file.removeChangeListener(mGoogleApiClient, changeListener);

In addition to adding the listener to the file or folder you wish to receive events for, your application must also implement the callback (changeListener in this example) you passed in the addChangeListener method call. The following demonstrates a callback that prints the received events to a log.

/**
 * A listener to handle file change events.
 */
final private ChangeListener changeListener = new ChangeListener() {
    @Override
    public void onChange(ChangeEvent event) {
        mLogTextView.setText(String.format("File change event: %s", event));
    }
};

For callbacks that take action, such as retrieving the latest version, you can use the hasContentChanged and hasMetadataChanged methods to determine whether the file contents, metadata, or both have changed.

For a full working example, see the ListenChangeEventsForFilesActivity sample in the Google Drive Android API Demos app.

Change Subscriptions

Change subscriptions allow your application to receive ChangeEvents even when it is not running. For example your application could show a notification when a file or folder your it cares about has changed, even if that change occurs while your application not running.

You can add a change subscription by calling the addChangeSubscription method of the file or folder you want to receive notifications for. The subscription is persistent until the next device reboot and will be active until the removeChangeSubscription method is called on the same resource.

Add a change subscription

The following example code shows the logic you might implement to add a change subscription to a file:

DriveFile file = Drive.DriveApi.getFile(mGoogleApiClient,
        mSelectedFileId);
file.addChangeSubscription(mGoogleApiClient);

Conversely, to remove a change subscription from a file, you can use code similar to the following example:

DriveFile file = Drive.DriveApi.getFile(mGoogleApiClient,
        mSelectedFileId);
file.removeChangeListener(mGoogleApiClient);

Handle change subscriptions events

To handle change subscription events you must create a class that extends DriveEventService and overrides the onChange method. This is where application-specific processing of the ChangeEvent would occur.

public class MyDriveEventService extends DriveEventService {

    @Override
    public void onChange(ChangeEvent event) {
        Log.d(TAG, event.toString());
        // Application-specific handling of event.
    }

}

The service must be defined in your application's manifest. In AndroidManifest.xml, create an entry for your service as a child of the application element.

<application ...>
    ...
    <service android:name=".MyDriveEventService" android:exported="true">
        <intent-filter>
            <action android:name="com.google.android.gms.drive.events.HANDLE_EVENT"/>
        </intent-filter>
    </service>
</application>

Your application will receive a ChangeEvent whenever an file or folder gets updated by another application on the device or the Drive API downloads changes from the server.