gpt4 book ai didi

android - FileObserver 修改不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:27:33 24 4
gpt4 key购买 nike

这是我的代码:

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

最佳答案

也许你写onEvent的方式不对,使用

if (!event.equals(MODIFY)) { return;} 

//the code you want
if (path.equals(blah blah blah)) {
//some code..
}

这是我在 FileObserver 中使用的方式,试试吧......

关于android - FileObserver 修改不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16858377/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com