Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)
Android APIs
public abstract class

FileObserver

extends Object
java.lang.Object
   ↳ android.os.FileObserver

Class Overview

Monitors files (using inotify ) to fire an event after files are accessed or changed by by any process on the device (including this one). FileObserver is an abstract class; subclasses must implement the event handler onEvent(int, String) .

Each FileObserver instance monitors a single file or directory. If a directory is monitored, events will be triggered for all files and subdirectories inside the monitored directory.

An event mask is used to specify which changes or actions to report. Event type constants are used to describe the possible changes in the event mask as well as what actually happened in event callbacks.

Warning : If a FileObserver is garbage collected, it will stop sending events. To ensure you keep receiving events, you must keep a reference to the FileObserver instance from some other live object.

Summary

Constants
int ACCESS Event type: Data was read from a file
int ALL_EVENTS Event mask: All valid event types, combined
int ATTRIB Event type: Metadata (permissions, owner, timestamp) was changed explicitly
int CLOSE_NOWRITE Event type: Someone had a file or directory open read-only, and closed it
int CLOSE_WRITE Event type: Someone had a file or directory open for writing, and closed it
int CREATE Event type: A new file or subdirectory was created under the monitored directory
int DELETE Event type: A file was deleted from the monitored directory
int DELETE_SELF Event type: The monitored file or directory was deleted; monitoring effectively stops
int MODIFY Event type: Data was written to a file
int MOVED_FROM Event type: A file or subdirectory was moved from the monitored directory
int MOVED_TO Event type: A file or subdirectory was moved to the monitored directory
int MOVE_SELF Event type: The monitored file or directory was moved; monitoring continues
int OPEN Event type: A file or directory was opened
Public Constructors
FileObserver ( String path)
Equivalent to FileObserver(path, FileObserver.ALL_EVENTS).
FileObserver ( String path, int mask)
Create a new file observer for a certain file or directory.
Public Methods
abstract void onEvent (int event, String path)
The event handler, which must be implemented by subclasses.
void startWatching ()
Start watching for events.
void stopWatching ()
Stop watching for events.
Protected Methods
void finalize ()
Invoked when the garbage collector has detected that this instance is no longer reachable.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final int ACCESS

Added in API level 1

Event type: Data was read from a file

Constant Value: 1 (0x00000001)

public static final int ALL_EVENTS

Added in API level 1

Event mask: All valid event types, combined

Constant Value: 4095 (0x00000fff)

public static final int ATTRIB

Added in API level 1

Event type: Metadata (permissions, owner, timestamp) was changed explicitly

Constant Value: 4 (0x00000004)

public static final int CLOSE_NOWRITE

Added in API level 1

Event type: Someone had a file or directory open read-only, and closed it

Constant Value: 16 (0x00000010)

public static final int CLOSE_WRITE

Added in API level 1

Event type: Someone had a file or directory open for writing, and closed it

Constant Value: 8 (0x00000008)

public static final int CREATE

Added in API level 1

Event type: A new file or subdirectory was created under the monitored directory

Constant Value: 256 (0x00000100)

public static final int DELETE

Added in API level 1

Event type: A file was deleted from the monitored directory

Constant Value: 512 (0x00000200)

public static final int DELETE_SELF

Added in API level 1

Event type: The monitored file or directory was deleted; monitoring effectively stops

Constant Value: 1024 (0x00000400)

public static final int MODIFY

Added in API level 1

Event type: Data was written to a file

Constant Value: 2 (0x00000002)

public static final int MOVED_FROM

Added in API level 1

Event type: A file or subdirectory was moved from the monitored directory

Constant Value: 64 (0x00000040)

public static final int MOVED_TO

Added in API level 1

Event type: A file or subdirectory was moved to the monitored directory

Constant Value: 128 (0x00000080)

public static final int MOVE_SELF

Added in API level 1

Event type: The monitored file or directory was moved; monitoring continues

Constant Value: 2048 (0x00000800)

public static final int OPEN

Added in API level 1

Event type: A file or directory was opened

Constant Value: 32 (0x00000020)

Public Constructors

public FileObserver ( String path)

Added in API level 1

Equivalent to FileObserver(path, FileObserver.ALL_EVENTS).

public FileObserver ( String path, int mask)

Added in API level 1

Create a new file observer for a certain file or directory. Monitoring does not start on creation! You must call startWatching() before you will receive events.

Parameters
path The file or directory to monitor
mask The event or events (added together) to watch for

Public Methods

public abstract void onEvent (int event, String path)

Added in API level 1

The event handler, which must be implemented by subclasses.

This method is invoked on a special FileObserver thread. It runs independently of any threads, so take care to use appropriate synchronization! Consider using post(Runnable) to shift event handling work to the main thread to avoid concurrency problems.

Event handlers must not throw exceptions.

Parameters
event The type of event which happened
path The path, relative to the main monitored file or directory, of the file or directory which triggered the event

public void startWatching ()

Added in API level 1

Start watching for events. The monitored file or directory must exist at this time, or else no events will be reported (even if it appears later). If monitoring is already started, this call has no effect.

public void stopWatching ()

Added in API level 1

Stop watching for events. Some events may be in process, so events may continue to be reported even after this method completes. If monitoring is already stopped, this call has no effect.

Protected Methods

protected void finalize ()

Added in API level 1

Invoked when the garbage collector has detected that this instance is no longer reachable. The default implementation does nothing, but this method can be overridden to free resources.

Note that objects that override finalize are significantly more expensive than objects that don't. Finalizers may be run a long time after the object is no longer reachable, depending on memory pressure, so it's a bad idea to rely on them for cleanup. Note also that finalizers are run on a single VM-wide finalizer thread, so doing blocking work in a finalizer is a bad idea. A finalizer is usually only necessary for a class that has a native peer and needs to call a native method to destroy that peer. Even then, it's better to provide an explicit close method (and implement Closeable ), and insist that callers manually dispose of instances. This works well for something like files, but less well for something like a BigInteger where typical calling code would have to deal with lots of temporaries. Unfortunately, code that creates lots of temporaries is the worst kind of code from the point of view of the single finalizer thread.

If you must use finalizers, consider at least providing your own ReferenceQueue and having your own thread process that queue.

Unlike constructors, finalizers are not automatically chained. You are responsible for calling super.finalize() yourself.

Uncaught exceptions thrown by finalizers are ignored and do not terminate the finalizer thread. See Effective Java Item 7, "Avoid finalizers" for more.