我正在使用FileObserver来观察目录的变化。该过程在 90% 的情况下都能正常工作,但偶尔会失败。
这是 logcat 的一个工作示例:
04-23 21:12:03.873: V/ItemObserver(1663): Setting up new item observer for item 2
04-23 21:12:04.374: I/ItemObserver(1663): Received item event for item 2, event: 256, file: batch.get.47
04-23 21:12:07.866: I/ItemObserver(1663): Received item event for item 2, event: 512, file: batch.get.47
04-23 21:12:07.873: I/ItemObserver(1663): Received item event for item 2, event: 512, file: item.xml
04-23 21:12:07.883: I/ItemObserver(1663): Received item event for item 2, event: 256, file: item.xml
04-23 21:12:08.033: I/ItemObserver(1663): Received item event for item 2, event: 8, file: item.xml
这是一个失败的例子:
04-23 22:08:09.403: V/ItemObserver(1751): Setting up new item observer for item 2
04-23 22:08:09.813: I/ItemObserver(1751): Received item event for item 2, event: 256, file: batch.get.52
04-23 22:08:09.954: I/ItemObserver(1751): Received item event for item 2, event: 32768, file: null
一旦我收到带有空文件的 32768 事件,一切都会停止。我检查了 FileObserver 的来源并搜索了 inotify 32768,但找不到在任何地方引用它的位置。
设置观察者的代码如下:
itemDirObserver = new FileObserver(getItemsCache().getProcessedItemDir(itemId).getPath(),
FileObserver.CLOSE_WRITE | FileObserver.CREATE | FileObserver.DELETE) {
@Override
public void onEvent(int event, final String file) {
itemDirChanged(event, file);
}
};
itemDirObserver.startWatching();
logcat 的代码是:
public synchronized void itemDirChanged(int event, String file) {
Log.i(LOG, "Received item event for item " + itemId + ", event: " + event + ", file: " + file);
switch (event) {
<snip>
知道 32768 和空文件是什么意思吗?