文件夹变化监听

publicclassRecursiveFileObserverextendsFileObserver{
        /** Only modification events */
        publicstaticint CHANGES_ONLY = CREATE | DELETE | CLOSE_WRITE | MOVE_SELF | MOVED_FROM | MOVED_TO;
       
        List<SingleFileObserver> mObservers;
        String mPath;
        int mMask;
 
        publicRecursiveFileObserver(String path){
                this(path, ALL_EVENTS);
        }
       
        publicRecursiveFileObserver(String path,int mask){
                super(path, mask);
                mPath = path;
                mMask = mask;
        }
 
        @Override
        publicvoid startWatching(){
                if(mObservers !=null)return;
               
                mObservers =newArrayList<SingleFileObserver>();
                Stack<String> stack =newStack<String>();
                stack.push(mPath);
               
                while(!stack.isEmpty()){
                        String parent = stack.pop();
                        mObservers.add(newSingleFileObserver(parent, mMask));
                        File path =newFile(parent);
                        File[] files = path.listFiles();
                        if(null== files)continue;
                        for(File f : files)
                        {
                                if(f.isDirectory()&&!f.getName().equals(“.”)&&!f.getName().equals(“..”)){
                                        stack.push(f.getPath());
                                }
                        }
                }
               
                for(SingleFileObserver sfo : mObservers){
                        sfo.startWatching();
                }
        }
 
        @Override
        publicvoid stopWatching(){
                if(mObservers ==null)return;
               
                for(SingleFileObserver sfo : mObservers){
                        sfo.stopWatching();
                }
                mObservers.clear();
                mObservers =null;
        }
 
        @Override
        publicvoid onEvent(int event,String path){
                switch(event)
                {
                caseFileObserver.ACCESS:
                        Log.i(“RecursiveFileObserver”,“ACCESS: “+ path);
                        break;
                caseFileObserver.ATTRIB:
                        Log.i(“RecursiveFileObserver”,“ATTRIB: “+ path);
                        break;
                caseFileObserver.CLOSE_NOWRITE:
                        Log.i(“RecursiveFileObserver”,“CLOSE_NOWRITE: “+ path);
                        break;
                caseFileObserver.CLOSE_WRITE:
                        Log.i(“RecursiveFileObserver”,“CLOSE_WRITE: “+ path);
                        break;
                caseFileObserver.CREATE:
                        Log.i(“RecursiveFileObserver”,“CREATE: “+ path);
                        break;
                caseFileObserver.DELETE:
                        Log.i(“RecursiveFileObserver”,“DELETE: “+ path);
                        break;
                caseFileObserver.DELETE_SELF:
                        Log.i(“RecursiveFileObserver”,“DELETE_SELF: “+ path);
                        break;
                caseFileObserver.MODIFY:
                        Log.i(“RecursiveFileObserver”,“MODIFY: “+ path);
                        break;
                caseFileObserver.MOVE_SELF:
                        Log.i(“RecursiveFileObserver”,“MOVE_SELF: “+ path);
                        break;
                caseFileObserver.MOVED_FROM:
                        Log.i(“RecursiveFileObserver”,“MOVED_FROM: “+ path);
                        break;
                caseFileObserver.MOVED_TO:
                        Log.i(“RecursiveFileObserver”,“MOVED_TO: “+ path);
                        break;
                caseFileObserver.OPEN:
                        Log.i(“RecursiveFileObserver”,“OPEN: “+ path);
                        break;
                default:
                        Log.i(“RecursiveFileObserver”,“DEFAULT(“+ event +“): “+ path);
                        break;
                }
        }
       
        /**
         * Monitor single directory and dispatch all events to its parent, with full path.
         * @author              uestc.Mobius <mobius@toraleap.com>
         * @version     2011.0121
         */
        classSingleFileObserverextendsFileObserver{
                String mPath;
               
                publicSingleFileObserver(String path){
                        this(path, ALL_EVENTS);
                        mPath = path;
                }
               
                publicSingleFileObserver(String path,int mask){
                        super(path, mask);
                        mPath = path;
                }
 
                @Override
                publicvoid onEvent(int event,String path){
                        String newPath = mPath +“/”+ path;
                        RecursiveFileObserver.this.onEvent(event, newPath);
                }
        }

}

注意:
1、必须保持FileObserver的引用才能监听文件,因此最好放到service中,并且把引用都保存起来;
2、FileObserver需要给每个文件夹设置监听器,因为它自己不能递归监听。

    原文作者:罗小姿
    原文地址: https://www.cnblogs.com/lyz459/p/3345660.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞