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 OnChangeListener 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 DriveResourceClient.addChangeListener method, passing in the file or folder you want to receive notifications for. Listeners are only active for the duration of the current connection or until you call the DriveResourceClient.removeChangeListener method.

The following example code demonstrates how to add a change listener for a file:

        .addChangeListener(file, changeListener)
        .addOnSuccessListener(this, new OnSuccessListener<ListenerToken>() {
            public void onSuccess(ListenerToken listenerToken) {
                mChangeListenerToken = listenerToken;

To remove a change listener from a file, you must call the DriveResourceClient.removeChangeListener method, passing in the ListenerToken object returned by DriveResourceClient.addChangeListener.

The following code sample demonstrates how to remove a change listener applied to a Drive resource:


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 OnChangeListener changeListener = new OnChangeListener() {
    public void onChange(ChangeEvent event) {
        mLogTextView.append(getString(R.string.change_event, 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.

Change subscriptions

Change subscriptions allow your application to receive ChangeEvent notices even when it is not running. For example, your application can show a notification when a file or folder it cares about has changed, even if the change occurs after the user has closed your app.

You can add a change subscription by calling the DriveResourceClient.addChangeSubscription method, passing in a reference to the file or folder you want to receive notifications for. The subscription remains until the next device reboot and stays active until the DriveResourceClient.removeChangeSubscription method is called for 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:

        new OnSuccessListener<Void>() {
            public void onSuccess(Void aVoid) {

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

Send feedback about...

Drive API for Android
Drive API for Android