使用FileObserver时遇到问题。 我想捕获事件FileObserver.CREATE路径是storage / emulated / 0 /
这是我的代码:
public class PathFileObserver extends FileObserver {
static final String TAG = PathFileObserver.class.getSimpleName();
/**
* should be end with File.separator
*/
String rootPath;
static final int mask = (FileObserver.CREATE);
public PathFileObserver(String root) {
super(root, mask);
if (!root.endsWith(File.separator)) {
root += File.separator;
}
rootPath = root;
}
@Override
public void onEvent(int event, String path) {
switch (event) {
case FileObserver.CREATE:
Log.d(TAG, "CREATE:" + rootPath + path);
break;
default:
// just ignore
break;
}
}
}
public class FileObserverService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
initial();
return START_STICKY;
}
private void initial() {
PathFileObserver pathFileObserver = new PathFileObserver(Environment.getExternalStorageDirectory().getPath());
pathFileObserver.startWatching();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
我在nexus 5上测试了它,操作系统版本为6.0。
如果有人能解决我的问题,请告诉我。