Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
550 views
in Technique[技术] by (71.8m points)

Android: FileObserver monitors only top directory

According to the documentation,

"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."

My code goes like,

    FileObserver fobsv = new FileObserver("/mnt/sdcard/") {

    @Override
    public void onEvent(int event, String path) {
        System.out.println(event+"    "+path);
    }
    };
    fobsv.startWatching();

However, the onEvent() is triggering only when a file is changed in the /mnt/sdcard/. If I create a file in /mnt/sdcard/downloads/, the method is not getting fired.

Is there any problem with the code?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is an open-source RecursiveFileObserver that works just as the normal FileObserver should ... I am using it currently it is what it is named , it acts as a FileObserver that is recursive for all directories beneath the directory you chose ...

Here is it :

public class RecursiveFileObserver extends FileObserver {

public static int CHANGES_ONLY = CLOSE_WRITE | MOVE_SELF | MOVED_FROM;

List<SingleFileObserver> mObservers;
String mPath;
int mMask;

public RecursiveFileObserver(String path) {
    this(path, ALL_EVENTS);
}

public RecursiveFileObserver(String path, int mask) {
    super(path, mask);
    mPath = path;
    mMask = mask;
}

@Override
public void startWatching() {
    if (mObservers != null) return;
    mObservers = new ArrayList<SingleFileObserver>();
    Stack<String> stack = new Stack<String>();
    stack.push(mPath);

    while (!stack.empty()) {
        String parent = stack.pop();
        mObservers.add(new SingleFileObserver(parent, mMask));
        File path = new File(parent);
        File[] files = path.listFiles();
        if (files == null) continue;
        for (int i = 0; i < files.length; ++i) {
            if (files[i].isDirectory() && !files[i].getName().equals(".")
                && !files[i].getName().equals("..")) {
                stack.push(files[i].getPath());
            }
        }
    }
    for (int i = 0; i < mObservers.size(); i++)
        mObservers.get(i).startWatching();
}

@Override
public void stopWatching() {
    if (mObservers == null) return;

    for (int i = 0; i < mObservers.size(); ++i)
        mObservers.get(i).stopWatching();

    mObservers.clear();
    mObservers = null;
}

@Override
public void onEvent(int event, String path) {

}

private class SingleFileObserver extends FileObserver {
    private String mPath;

    public SingleFileObserver(String path, int mask) {
        super(path, mask);
        mPath = path;
    }

    @Override
    public void onEvent(int event, String path) {
        String newPath = mPath + "/" + path;
        RecursiveFileObserver.this.onEvent(event, newPath);
    } 

}
}

Make a new class in your app and copy this code to it , and use it as you like ! Vote up if you find this helpful !


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...