我要聽文件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