The reason of this is that android does not abstract over underlying file system well enough and returns underlying event code with some of the flags raised (some of the higher bits of the event
). This is why comparing the event
value with the event type directly does not work.
To solve this you can drop extra flags by applying FileObserver.ALL_EVENTS
event mask (using bitwise and
) to actual event
value stripping it down to event type.
Using the code you've provided in your question this will look something like this:
private final class DirectoryObserver extends FileObserver {
private DirectoryObserver(String path, int mask) {
super(path, mask);
}
@Override
public void onEvent(int event, String pathString) {
event &= FileObserver.ALL_EVENTS;
switch (event) {
case FileObserver.DELETE_SELF:
//do stuff
break;
case FileObserver.CREATE:
case FileObserver.DELETE:
//do stuff
break;
}
}
}