好的,这很简单:我有一个FileObserver类来观察带有音乐的文件夹.所以我实现了onEvent和所有这些东西,但当我使用文件管理器移动或粘贴该文件夹上的文件时,而不是获取FileObserver.MOVED_TO或FileObserver.CREATE,我得到的奇怪事件的数字如1073741656,未记录在:http: //developer.android.com/reference/android/os/FileObserver.html
那么如何获取删除,移动,创建和粘贴等特定事件?
[编辑]这是代码:
private class MusicsFileObserver extends FileObserver {
public MusicsFileObserver(String root) {
super(root);
if (!root.endsWith(File.separator)) {
root += File.separator;
}
}
@SuppressWarnings("unused")
public void close() {
super.finalize();
}
public void onEvent(final int event, String path) {
//here is the problem, if you see the documentation, when a file is moved
//to this directory, event should be equal to FileObserver.MOVED_TO,
//a constant value of 128. But when debugging, instead of entering here one time
//with event == 128, this method onEvent is being called 4~5 times with event
//with numbers like 1073741656
if (event != FileObserver.ACCESS || event != FileObserver.OPEN || event != 32768)
runOnUiThread(new Runnable() {
public void run() {
rescanMusics();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
mvs*_*es2 12
对于遇到此问题的其他任何人,我发现MOVED_TO和MOVED_FROM事件在事件标志中打开了高位.MOVED_FROM是0x40000040,MOVED_TO是0x40000080.解决方法是简单地使用事件代码'和'ALL_EVENTS来关闭高位,即"event&= FileObserver.ALL_EVENTS"
更新:我发现你可以从http://rswiki.csie.org/lxr/http/source/include/linux/inotify.h?a=m68k#L45获得inotify标志,如果google添加这些标志会很好FileObserver doc的位标志.
观察者事件类型如下:
public void onEvent(int event) {
if ((FileObserver.CREATE & event)!=0) {
// do what ever you want.
} else if ((FileObserver.MODIFY & event)!=0) {
// do what ever you want.
} ...... etc
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3314 次 |
| 最近记录: |