我要听文件system.I发生变化之后的工作现在用 FileObserver.Here是我的代码:文件观察者停止一段时间
代码:
class MyDirObserver extends FileObserver {
String superPath;
public MyDirObserver(String path) {
super(path, ALL_EVENTS);
this.superPath = path;
}
public void onEvent(int event, String path) {
Log.e("onEvent of Directory", "=== onEvent ===");
try {
_Dump("dir", event, path, superPath);
} catch (NullPointerException ex) {
Log.e("ERROR", "I am getting error");
}
}
}
private void _Dump(final String tag, int event, String path, String superPath) {
Log.d(tag, "=== dump begin ===");
Log.d(tag, "path=" + path);
Log.d(tag, "super path=" + superPath);
Log.d(tag, "event list:");
if ((event & FileObserver.OPEN) != 0) {
Log.d(tag, " OPEN");
}
if ((event & FileObserver.CLOSE_NOWRITE) != 0) {
Log.d(tag, " CLOSE_NOWRITE");
}
if ((event & FileObserver.CLOSE_WRITE) != 0) {
Log.d(tag, " CLOSE_WRITE");
Log.i("NEWFILEOBSERVER", "File is Modified");
if (path != null) {
Log.d("---------FilePath", superPath + path);
}
}
if ((event & FileObserver.CREATE) != 0) {
isCreate = true;
Log.i("NEWFILEOBSERVER", "File is Created ");
if (path != null) {
Log.d("---------FilePath", superPath + path);
}
Log.d(tag, " CREATE");
}
if ((event & FileObserver.DELETE) != 0) {
Log.i("NEWFILEOBSERVER", "File is deleted");
if (path != null) {
Log.d("---------FilePath", superPath + path);
}
// startMyActivity("A new file is deleted thats="+superPath);
Log.d(tag, " DELETE");
}
if ((event & FileObserver.DELETE_SELF) != 0) {
Log.d(tag, " DELETE_SELF");
}
if ((event & FileObserver.ACCESS) != 0) {
Log.d(tag, " ACCESS");
}
if ((event & FileObserver.MODIFY) != 0) {
if (!isModified)
isModified = true;
if (isModified && isOpen)
isAgainModified = true;
Log.d(tag, " MODIFY");
}
if ((event & FileObserver.MOVED_FROM) != 0) {
Log.d(tag, " MOVED_FROM");
if (path != null) {
Log.d("---------FilePath", superPath + path);
}
}
if ((event & FileObserver.MOVED_TO) != 0) {
Log.d(tag, " MOVED_TO");
if (path != null) {
Log.d("---------FilePath", superPath + path);
}
}
if ((event & FileObserver.MOVE_SELF) != 0) {
Log.d(tag, " MOVE_SELF");
}
if ((event & FileObserver.ATTRIB) != 0) {
Log.d(tag, " ATTRIB");
}
Log.d(tag, "=== dump end ===");
}
它经过一番停止time.I没有得到确切的时间,但没有 总是工作,虽然我在一个循环中调用startWatching()服务,该循环运行sdcard的所有文件夹,并为每个文件夹调用startWatching()。它显示不可预知的行为,并停止监听某些文件夹,并完美地运行其他文件夹。
我希望你们帮助我。我尝试了很多方法,但它完美地不起作用 。难道我做错了什么?或者还有其他方式可以做到这一点。
你确定观察员还活着吗?他们运行什么线程?也许他们被垃圾堆砌机毁坏了? – cboese 2010-03-25 23:42:30
你有没有得到任何解决方案? – 2016-09-20 05:58:49