这是我的代码:
public class SyncNotifyService extends Service {
private final static String TAG = "FileService";
SDCardListener fileObserver = null;
@Override
public IBinder onBind(Intent intent) {
return null;
}
public File getCacheDir() {
if (!StorageUtil.isExternalStorageAvailable()) {
return null;
}
File dir = new File(Environment.getExternalStorageDirectory(), "Cache");
return dir;
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate");
fileObserver = new SDCardListener(FileCache.getCacheDir().getPath(), FileObserver.MODIFY);
fileObserver.startWatching();
}
class SDCardListener extends FileObserver {
public SDCardListener(String path, int mask) {
super(path, mask);
}
@Override
public void onEvent(int event, String path) {
final int action = event & FileObserver.ALL_EVENTS;
switch (action) {
case FileObserver.MODIFY:
Log.d(TAG, "event: MODIFY");
break;
}
}
}
}
嗨,我使用此代码通知目录。但我发现它从不调用 onEvent 使用 FileObserver.MODIFY 参数,有人知道如何编写正确的代码吗?我的安卓版本是 4.1.1