有没有办法检测何时在Android手机中创建或修改或下载文件?我检查了可用的意图,但没有一个与文件有关。我正在研究广播接收器,并无法找到一种方法来检测新文件的创建。有人可以帮忙吗?
答案 0 :(得分:0)
使用FileObserver ...
public class MyFileObserver extends FileObserver {
public MyFileObserver(String path) {
super(path);
}
@Override
public void onEvent(int event, String path) {
switch (event) {
case DELETE:
// file deleted
break;
case ACCESS:
// file accessed;
break;
case MODIFY:
// file modified;
break;
case OPEN:
// file opened
break;
// see documentation for available events
default:
break;
}
}
}
样本用法:
MyFileObserver fileObserver = new MyFileObserver(Environment.getExternalStorageDirectory().getPath());
fileObserver.startWatching();