Android FileObserver is an interface provided by Android OS to monitor file access and modification. It’s based on the Linux inotify file change notification system.
This blog will give a brief introduction to inotify and illustrate the usage of Android FileObserver API with an example.
inotify is a simple but powerful file change notification system. It’s a mechanism built into the Linux 2.6.13 kernel and above.
The mechanism is exposed to the userspace program through a set of 3 system calls, including inotify_init, inotify_add_watch and inotify_rm_watch.
These system calls are declared in /usr/include/sys/inotify.h. One needs to include the header file as below in order to use them.
The inotify_init function has the following prototype,
This function will create an inotify instance and return a file descriptor which all events are read from. Each instance is associated with a unique, ordered queue for events.
The inotify_add_watch function is declared as,
where __fd is the file descriptor returned from the inotify_init function, __name is the path to the object (either file or directory) to watch, and __mask indicates the types of event to watch. The return value can be used to refer to the watch added, called watch descriptor.
The inotify_rm_watch system call is declared as,
where __fd is the return value of inotify_init, and __wd is the return value of inotify_add_watch. This function will remove the watch from the queue.
With this set of system call and normal file IO operations, one can monitor the file changes in Linux system. Please refer reference 1 and 2 for further details of inotify.
Android FileObserver is based on FileObserver. It provides similar monitoring mechanism as inotify does. One thing worth-mentioning is that the API documentation says “If a directory is monitored, events will be triggered for all files and subdirectories (recursively) inside the monitored directory”, but the FileObserver API is actually not recursive.
In other words, a FileObserver is attached to a folder, then only the files and sub-folders inside it are monitored, but its sub sub-folders and files are not. For example, there is a folder B inside folder A, and a folder C inside folder B. If a FileObserver is created to monitor folder A, then any modifications done for folder B is detected, but not for folder C.
An Android Sample App Based on FileObserver
As FileObserver is an abstract class, one must create a subclass that extends FileObserver and implement the event handler onEvent(int, string).
Below is an example,
Once the subclass is defined, one needs to create instances of this class in order to use it.
Similar to inotify_add_watch and inotify_remove_watch, one can call startWatching() and stopWatching() methods to start or stop monitoring.
For a complete sample app which monitors the /sdcard directory of the Android OS, and output the results to screen, you can refer to my github repo here, or simply click here to download the source code zip file.
Below is a screenshot of the sample app.
Figure 1. Screenshot of File Modification Monitor
1. Kernel Korner – Intro to inotify: http://www.linuxjournal.com/article/8478
2. inotify – a powerful yet simple file change notification system: http://www.kernel.org/doc/Documentation/filesystems/inotify.txt
3. Android FileObserver API documentation: http://developer.android.com/reference/android/os/FileObserver.html