gpt4 book ai didi

java - android fileObserver onEvent 未被调用

转载 作者:行者123 更新时间:2023-11-30 11:03:16 25 4
gpt4 key购买 nike

我正在尝试确定文件何时被修改。目前我有一个类文件观察者类,其中包含

public class fileObserver extends FileObserver {
public String absolutePath;
public fileObserver(String path) {
super(path, FileObserver.ALL_EVENTS);
absolutePath = path;
}

@Override
public void onEvent(int event, String path) {
if (path == null) {
return;
}
if (event == FileObserver.MODIFY) {
Log.d("change","changed");
}
}
}

在我的主要 Activity 中我有

fileObserver test = new fileObserver(fullpathnamehere);
test.startWatching();

fullpathname 这里是我试图观察的文件的文件路径。我检查过,文件路径有效。谁能告诉我我做错了什么?

最佳答案

方法 onEvent() 没有被调用,因为路径不正确:

event fileObserver test = new fileObserver(fullpathnamenere);
test.startWatching();

检查你的文件路径是否正确!

尝试使用:

String fullpathnamenere = Environment.getExternalStorageDirectory().getPath() + "/stuff.txt";

更新:

我发现(感谢 Mark Murphy 先生)FileObserver 不是递归的!

检查这个 https://code.google.com/p/android/issues/detail?id=33659

我建议使用RecursiveFileObserver<强> https://github.com/owncloud/android/blob/master/src/com/owncloud/android/utils/RecursiveFileObserver.java

关于java - android fileObserver onEvent 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30489189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com