我需要在手机启动后一直跟踪从整个文件系统中删除的图像和视频(即使我的 UI 应用程序未运行),直到其关闭。为此,我使用服务和 FileObserver。不幸的是,下面附加的代码不起作用(我没有使用调试器来获取该方法
public void onEvent(int 事件,字符串文件)
虽然我对 FS 做了一些更改...服务启动了并且显然执行了 onStarted() 方法...
请帮忙
public class DataChangeListener extends Service {
private static Context context;
@Override
public IBinder onBind(Intent intent) {
context = this;
return null;
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
context = this;
FileObserver observer = new FileObserver("/") {
@Override
public void onEvent(int event, String file) {
System.out.println();
}
};
observer.startWatching();
}
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
}
最佳答案
I need to track removed images and videos from the whole file system all the time
这是不可能的,除非您创建操作系统的修改版本,因为您将无法保持服务始终运行。例如,用户可以随时删除您的服务。
Unfortunately the code attached below doesn't work
首先,您没有对 /
的读取权限,这可能会干扰 FileObserver
操作。
其次,您错过了突出显示的段落 in the FileObserver
documentation :
Warning: If a FileObserver is garbage collected, it will stop sending events. To ensure you keep receiving events, you must keep a reference to the FileObserver instance from some other live object.
您允许对 FileObserver
进行垃圾收集。
关于java - Android:FileObserver 不会收到有关 FS 更改的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13521464/