android中FileObserver的运用
来源:互联网 发布:坏种 知乎 编辑:程序博客网 时间:2024/05/16 10:25
FileObserver,顾名思义,就是文件(夹)观察者,是用来监听文件(夹)的实时变化的,其底层是由linux平台的inotify机制实现的,有兴趣的同学可以自行研究。
那么android中FileObserver的具体用法如何呢?我首次接触是在PackageMangaerService的构造函数中,他被用来监视文件夹(data/app,system/app等),其本身也不难理解,我们只需要集成并重写几个方法就好了。
其中必须重写的有带一个String类型的构造函数已经 onEvent(int event, String path) 。
构造函数必须传入String类型的path值,也就是被监视的文件(夹)路径
onEvent方法则是在监测文件的各种变化。
FileObserver中提供了几种event值我们一起来看一下
/** Event type: Data was read from a file */ public static final int ACCESS = 0x00000001; /** Event type: Data was written to a file */ public static final int MODIFY = 0x00000002; /** Event type: Metadata (permissions, owner, timestamp) was changed explicitly */ public static final int ATTRIB = 0x00000004; /** Event type: Someone had a file or directory open for writing, and closed it */ public static final int CLOSE_WRITE = 0x00000008; /** Event type: Someone had a file or directory open read-only, and closed it */ public static final int CLOSE_NOWRITE = 0x00000010; /** Event type: A file or directory was opened */ public static final int OPEN = 0x00000020; /** Event type: A file or subdirectory was moved from the monitored directory */ public static final int MOVED_FROM = 0x00000040; /** Event type: A file or subdirectory was moved to the monitored directory */ public static final int MOVED_TO = 0x00000080; /** Event type: A new file or subdirectory was created under the monitored directory */ public static final int CREATE = 0x00000100; /** Event type: A file was deleted from the monitored directory */ public static final int DELETE = 0x00000200; /** Event type: The monitored file or directory was deleted; monitoring effectively stops */ public static final int DELETE_SELF = 0x00000400; /** Event type: The monitored file or directory was moved; monitoring continues */ public static final int MOVE_SELF = 0x00000800;
FileObserver fb = new FileObserver("/mnt/sdcard") {@Overridepublic void onEvent(int event, String path) {// TODO Auto-generated method stub}};//开启监听fb.startWatching();//结束监听fb.stopWatching();
当然,遗憾的是我们只能监听指定的根目录或某个文件,并不能一次监听到他所有的子目录及文件,目前的解决办法是通过遍历将所有文件夹及文件都添加监听,可是FileObserver其实是一个线程,这样大规模的添加势必对程序消耗太大。
- android中FileObserver的运用
- android中FileObserver的运用
- Android FileObserver
- Android FileObserver
- Android FileObserver
- Android FileObserver
- android FileObserver
- Android FileObserver
- Android中关于FileObserver类的监听文件和目录动态
- Android FileObserver 用法
- Android FileObserver 用法
- Android FileObserver 用法
- Android--文件监控FileObserver
- android FileObserver的用法与避坑指南
- android中全局变量的运用
- Android 中Notification的运用
- Android中BroadcastReceiver的运用
- Android中Spinner的运用
- 利用非递归方法实现二叉树的层次遍历 .(待完成!!!!!!)
- Linux下的多线程编程
- PES分析
- AVR和STM端口输出最高频率小测试
- HBase 在淘宝的应用和优化小结
- android中FileObserver的运用
- NoSQL 在腾讯应用实践
- 为什么Hadoop将一定会是分布式计算的未来
- java程序设计中有关内存管理的经验
- 用MongoDB实现MapReduce
- 嵌入式行业工程师应该这样规划人生!
- Android overlay 学习
- 体验假死循环
- Hadoop常见错误总结