安卓:FileObserver只监视顶级目录目录、FileObserver

2023-09-12 06:31:04 作者:今生今世つ

根据该文件,

 每个FileObserver实例监控单个文件或目录。如果一个目录被监控,
事件将触发对监控目录中的所有文件和子目录。
 

我的code是这样,

  FileObserver fobsv =新FileObserver(到/ mnt / SD卡/){

    @覆盖
    公共无效的onEvent(INT事件,字符串路径){
        的System.out.println(事件++路径);
    }
    };
    fobsv.startWatching();
 

不过,的onEvent()被触发,只有当一个文件中的的/ mnt / SD卡/ 被改变。如果我创建一个文件中的的/ mnt / SD卡/下载/ ,该方法不被解雇。

有没有用code什么问题?

解决方案   

根据文档

该文档是不正确,因为被记录在这个问题。

  

有没有用code什么问题?

串口转WiFi模块 HTTP指令使用示例

没有,但 FileObserver 不是递归,尽管文档与此相反。

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?

解决方案

According to the documentation

The documentation is incorrect, as is noted in this issue.

Is there any problem with the code?

No, but FileObserver is not recursive, despite the documentation to the contrary.