FileObserver
abstract class FileObserver
| kotlin.Any | |
| ↳ | android.os.FileObserver |
Monitors files (using inotify) to fire an event after files are accessed or changed by any process on the device (including this one). FileObserver is an abstract class; subclasses must implement the event handler onEvent(int,java.lang.String).
Each FileObserver instance can monitor multiple files or directories. 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 | |
|---|---|
| static Int |
Event type: Data was read from a file |
| static Int |
Event mask: All valid event types, combined. |
| static Int |
Event type: Metadata (permissions, owner, timestamp) was changed explicitly |
| static Int |
Event type: Someone had a file or directory open read-only, and closed it |
| static Int |
Event type: Someone had a file or directory open for writing, and closed it |
| static Int |
Event type: A new file or subdirectory was created under the monitored directory |
| static Int |
Event type: A file was deleted from the monitored directory |
| static Int |
Event type: The monitored file or directory was deleted; monitoring effectively stops |
| static Int |
Event type: Data was written to a file |
| static Int |
Event type: A file or subdirectory was moved from the monitored directory |
| static Int |
Event type: A file or subdirectory was moved to the monitored directory |
| static Int |
Event type: The monitored file or directory was moved; monitoring continues |
| static Int |
Event type: A file or directory was opened |
| Public constructors | |
|---|---|
FileObserver(file: File)Equivalent to FileObserver(file, FileObserver.ALL_EVENTS). |
|
FileObserver(file: File, mask: Int)Create a new file observer for a certain file or directory. |
|
FileObserver(path: String!)Equivalent to FileObserver(path, FileObserver.ALL_EVENTS). |
|
FileObserver(path: String!, mask: Int)Create a new file observer for a certain file or directory. |
|
FileObserver(files: MutableList<File!>)Equivalent to FileObserver(paths, FileObserver.ALL_EVENTS). |
|
FileObserver(files: MutableList<File!>, mask: Int)Version of |
|
| Public methods | |
|---|---|
| abstract Unit |
The event handler, which must be implemented by subclasses. |
| open Unit |
Start watching for events. |
| open Unit |
Stop watching for events. |
| Protected methods | |
|---|---|
| open Unit |
finalize()Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. |
Constants
ALL_EVENTS
static val ALL_EVENTS: Int
Event mask: All valid event types, combined.
Value is either 0 or a combination of the following:
ACCESSMODIFYATTRIBCLOSE_WRITECLOSE_NOWRITEOPENMOVED_FROMMOVED_TOCREATEDELETEDELETE_SELFMOVE_SELF
Value: 4095ATTRIB
static val ATTRIB: Int
Event type: Metadata (permissions, owner, timestamp) was changed explicitly
Value: 4CLOSE_NOWRITE
static val CLOSE_NOWRITE: Int
Event type: Someone had a file or directory open read-only, and closed it
Value: 16CLOSE_WRITE
static val CLOSE_WRITE: Int
Event type: Someone had a file or directory open for writing, and closed it
Value: 8CREATE
static val CREATE: Int
Event type: A new file or subdirectory was created under the monitored directory
Value: 256DELETE
static val DELETE: Int
Event type: A file was deleted from the monitored directory
Value: 512DELETE_SELF
static val DELETE_SELF: Int
Event type: The monitored file or directory was deleted; monitoring effectively stops
Value: 1024MOVED_FROM
static val MOVED_FROM: Int
Event type: A file or subdirectory was moved from the monitored directory
Value: 64MOVED_TO
static val MOVED_TO: Int
Event type: A file or subdirectory was moved to the monitored directory
Value: 128MOVE_SELF
static val MOVE_SELF: Int
Event type: The monitored file or directory was moved; monitoring continues
Value: 2048Public constructors
FileObserver
FileObserver(file: File)
Equivalent to FileObserver(file, FileObserver.ALL_EVENTS).
| Parameters | |
|---|---|
file |
File: This value cannot be null. |
FileObserver
FileObserver(
file: File,
mask: Int)
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 | |
|---|---|
file |
File: The file or directory to monitor. This value cannot be null. |
mask |
Int: The event or events (added together) to watch for. Value is either 0 or a combination of the following:
|
FileObserver
FileObserver(path: String!)
Deprecated: use FileObserver(java.io.File) instead.
Equivalent to FileObserver(path, FileObserver.ALL_EVENTS).
FileObserver
FileObserver(
path: String!,
mask: Int)
Deprecated: use FileObserver(java.io.File,int) instead.
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 |
String!: The file or directory to monitor |
mask |
Int: The event or events (added together) to watch for. Value is either 0 or a combination of the following:
|
FileObserver
FileObserver(files: MutableList<File!>)
Equivalent to FileObserver(paths, FileObserver.ALL_EVENTS).
| Parameters | |
|---|---|
files |
MutableList<File!>: The files or directories to monitor. This value cannot be null. |
FileObserver
FileObserver(
files: MutableList<File!>,
mask: Int)
Version of FileObserver(java.io.File,int) that allows callers to monitor multiple files or directories.
| Parameters | |
|---|---|
files |
MutableList<File!>: The files or directories to monitor. This value cannot be null. |
mask |
Int: The event or events (added together) to watch for. Value is either 0 or a combination of the following:
|
Public methods
onEvent
abstract fun onEvent(
event: Int,
path: String?
): Unit
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 Handler.post(Runnable) to shift event handling work to the main thread to avoid concurrency problems.
Event handlers must not throw exceptions.
| Parameters | |
|---|---|
event |
Int: The type of event which happened |
path |
String?: The path, relative to the main monitored file or directory, of the file or directory which triggered the event. This value can be null for certain events, such as MOVE_SELF. |
startWatching
open fun startWatching(): Unit
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.
stopWatching
open fun stopWatching(): Unit
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
finalize
protected open fun finalize(): Unit
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.
The general contract of finalize is that it is invoked if and when the Java virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized. The finalize method may take any action, including making this object available again to other threads; the usual purpose of finalize, however, is to perform cleanup actions before the object is irrevocably discarded. For example, the finalize method for an object that represents an input/output connection might perform explicit I/O transactions to break the connection before the object is permanently discarded.
The finalize method of class Object performs no special action; it simply returns normally. Subclasses of Object may override this definition.
The Java programming language does not guarantee which thread will invoke the finalize method for any given object. It is guaranteed, however, that the thread that invokes finalize will not be holding any user-visible synchronization locks when finalize is invoked. If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates.
After the finalize method has been invoked for an object, no further action is taken until the Java virtual machine has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded.
The finalize method is never invoked more than once by a Java virtual machine for any given object.
Any exception thrown by the finalize method causes the finalization of this object to be halted, but is otherwise ignored.
| Exceptions | |
|---|---|
java.lang.Throwable |
the Exception raised by this method |