FileObserverpublic abstract class FileObserver extends Object 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 {@link #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. |
Fields Summary |
---|
public static final int | ACCESSEvent type: Data was read from a file | public static final int | MODIFYEvent type: Data was written to a file | public static final int | ATTRIBEvent type: Metadata (permissions, owner, timestamp) was changed explicitly | public static final int | CLOSE_WRITEEvent type: Someone had a file or directory open for writing, and closed it | public static final int | CLOSE_NOWRITEEvent type: Someone had a file or directory open read-only, and closed it | public static final int | OPENEvent type: A file or directory was opened | public static final int | MOVED_FROMEvent type: A file or subdirectory was moved from the monitored directory | public static final int | MOVED_TOEvent type: A file or subdirectory was moved to the monitored directory | public static final int | CREATEEvent type: A new file or subdirectory was created under the monitored directory | public static final int | DELETEEvent type: A file was deleted from the monitored directory | public static final int | DELETE_SELFEvent type: The monitored file or directory was deleted; monitoring effectively stops | public static final int | MOVE_SELFEvent type: The monitored file or directory was moved; monitoring continues | public static final int | ALL_EVENTSEvent mask: All valid event types, combined | private static final String | LOG_TAG | private static ObserverThread | s_observerThread | private String | m_path | private Integer | m_descriptor | private int | m_mask |
Constructors Summary |
---|
public FileObserver(String path)Equivalent to FileObserver(path, FileObserver.ALL_EVENTS).
this(path, ALL_EVENTS);
| public FileObserver(String path, int mask)Create a new file observer for a certain file or directory.
Monitoring does not start on creation! You must call
{@link #startWatching()} before you will receive events.
m_path = path;
m_mask = mask;
m_descriptor = -1;
|
Methods Summary |
---|
protected void | finalize()
stopWatching();
| public abstract void | onEvent(int event, java.lang.String path)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 {@link Handler#post(Runnable)} to shift
event handling work to the main thread to avoid concurrency problems.
Event handlers must not throw exceptions.
| public void | startWatching()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.
if (m_descriptor < 0) {
m_descriptor = s_observerThread.startWatching(m_path, m_mask, this);
}
| public void | stopWatching()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.
if (m_descriptor >= 0) {
s_observerThread.stopWatching(m_descriptor);
m_descriptor = -1;
}
|
|