FileObserver gets weird events

Well, it's simple: I have a FileObserver class to watch a music folder. So I implemented onEvent and all that, but when I move or paste the file into this folder using the file manager, instead of getting the FileObserver.MOVED_TO or FileObserver.CREATE file, I get strange events with numbers like 1073741656 that are not documented: http : //developer.android.com/reference/android/os/FileObserver.html

So, how do I get these specific events, such as delete, move, create and insert?

[edit] Here is the code:

private class MusicsFileObserver extends FileObserver { public MusicsFileObserver(String root) { super(root); if (!root.endsWith(File.separator)) { root += File.separator; } } @SuppressWarnings("unused") public void close() { super.finalize(); } public void onEvent(final int event, String path) { //here is the problem, if you see the documentation, when a file is moved //to this directory, event should be equal to FileObserver.MOVED_TO, //a constant value of 128. But when debugging, instead of entering here one time //with event == 128, this method onEvent is being called 4~5 times with event //with numbers like 1073741656 if (event != FileObserver.ACCESS || event != FileObserver.OPEN || event != 32768) runOnUiThread(new Runnable() { public void run() { rescanMusics(); } }); } } 
+8
android
source share
4 answers

For everyone who encounters this, I have found that the MOVED_TO and MOVED_FROM events contain the high order bits included in the event flag. MOVED_FROM is 0x40000040, and MOVED_TO is 0x40000080. The workaround is simply "and" ALL_EVENTS with an event code to turn off high bits, that is, "event & = FileObserver.ALL_EVENTS"

Update: I found inotify flags that you can get from http://rswiki.csie.org/lxr/http/source/include/linux/inotify.h?a=m68k#L45 , it would be nice if Google added these bit flags in the FileObserver file.

+10
source share

Observer Event Type:

 public void onEvent(int event) { if ((FileObserver.CREATE & event)!=0) { // do what ever you want. } else if ((FileObserver.MODIFY & event)!=0) { // do what ever you want. } ...... etc } 
+2
source share

Try linking the link to Observer in your Application class. like this

 private ArrayList<FileObserver> mObservers = new ArrayList<FileObserver>(); public void addObserver(FileObserver observer){ mObservers.add(observer); } public void removeObserver(FileObserver observer){ mObservers.remove(observer); } 

It works for me!

0
source share

If anyone else comes from Google, remember this from the documentation :

startWatching ()

Start viewing events. A monitored file or directory must exist at this time, otherwise events will not be reported (even if it appears later). If monitoring is already running, the call has no effect.

0
source share

All Articles