2018-11-14 遇到的一点小问题
项目里需要监听某个文件夹内容变化,来做文件自动上传。首选就是android.os.FileObserverinotify,对,就是Linux上的inotify。具体做法网上一搜一大堆,懒得写了。
做这个时遇到的第一个问题就是重写public abstract void onEvent(int event, @Nullable String path);
方法,来监听事件变化时,发现int event
的值和预计的不一样。网上找了下,才知道还需要先进行计算int e = event & FileObserver.ALL_EVENTS;
才能正确获取。
然后顺便整理了一下相关含义,不知道对不对,自己懂就行。
int e = event & FileObserver.ALL_EVENTS;
switch (e) {
case FileObserver.ACCESS:
Log.e("wannoo", "文件操作___" + e + "__1打开文件后读取文件的操作");
break;
case FileObserver.MODIFY:
Log.e("wannoo", "文件操作___" + e + "__2文件被修改");
break;
case FileObserver.ATTRIB:
Log.e("wannoo", "文件操作___" + e + "__4属性变化");
break;
case FileObserver.CLOSE_WRITE:
Log.e("wannoo", "文件操作___" + e + "__8文件写入或编辑后关闭");
break;
case FileObserver.CLOSE_NOWRITE:
Log.e("wannoo", "文件操作___" + e + "__16只读文件被关闭");
break;
case FileObserver.OPEN:
Log.e("wannoo", "文件操作___" + e + "__32文件被打开");
break;
case FileObserver.MOVED_FROM:
Log.e("wannoo", "文件操作___" + e + "__64移出事件");//试了重命名先MOVED_FROM再MOVED_TO
break;
case FileObserver.MOVED_TO:
Log.e("wannoo", "文件操作___" + e + "__128移入事件");
break;
case FileObserver.CREATE:
Log.e("wannoo", "文件操作___" + e + "__256新建文件");//把文件移动给自己先CREATE在DELETE
break;
case FileObserver.DELETE:
Log.e("wannoo", "文件操作___" + e + "__512有删除文件");//把文件移出去DELETE
break;
case FileObserver.DELETE_SELF:
Log.e("wannoo", "文件操作___" + e + "__1024监听的这个文件夹被删除");
break;
case FileObserver.MOVE_SELF:
Log.e("wannoo", "文件操作___" + e + "__2048监听的这个文件夹被移走");
break;
case FileObserver.ALL_EVENTS:
Log.e("wannoo", "文件操作___" + e + "__4095全部操作");
break;
}
然后这是官方api注释,顺便贴一下,方便看:
/** 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;
/** Event mask: All valid event types, combined */
public static final int ALL_EVENTS = ACCESS | MODIFY | ATTRIB | CLOSE_WRITE | CLOSE_NOWRITE | OPEN | MOVED_FROM | MOVED_TO | DELETE | CREATE | DELETE_SELF | MOVE_SELF;