简体   繁体   English

Android FileObserver onEvent 未被调用

[英]Android FileObserver onEvent not getting called

Can someone possibly help me with this?有人可以帮我解决这个问题吗?

I want to observe a file to see if it gets modified so that I can update the activity.我想观察一个文件,看看它是否被修改,以便我可以更新活动。 After several tests, I've determined it's just plain not working.经过几次测试,我确定它只是简单地不起作用。 Am I doing something wrong?难道我做错了什么?

I'm creating a FileObserver with an onEvent method to display a Toast and log data just to see if it's working, however the onEvent is never getting called.我正在创建一个带有 onEvent 方法的 FileObserver 来显示 Toast 并记录数据,只是为了查看它是否正常工作,但是 onEvent 永远不会被调用。 I have tried it both with an existing and a new file, but it doesn't seem to work in either case.我已经对现有文件和新文件都进行了尝试,但似乎在任何一种情况下都不起作用。

    Context context = this;
    File fileFolder = context.getFilesDir();

    String fileName = "quest";
    FileObserver questObserver = new FileObserver(fileFolder.getPath()) { // also tried fileFolder.getName()
        @Override
        public void onEvent(int event, String path) {
            Toast.makeText(getApplicationContext(), "onEvent fired", Toast.LENGTH_LONG).show();
            Log.d(TAG, "FileObserver().onEvent");
        }
    };
    questObserver.startWatching();

    /* create file */
    ObjectOutputStream objectOut = null;
    try {
        FileOutputStream fileOut = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        objectOut = new ObjectOutputStream(fileOut);
        objectOut.writeObject(new Quest());
        fileOut.getFD().sync();
    } catch (IOException e) {
        Log.d(TAG, e.getMessage());
    } finally {
        if (objectOut != null) {
            try {
                objectOut.close();
            } catch (IOException e) {
                Log.d(TAG, e.getMessage());
            }
        }
    }

    /* read file */
    ObjectInputStream objectIn = null;
    Quest quest = null;

     try {
         FileInputStream fileIn = context.openFileInput(fileName);
         objectIn = new ObjectInputStream(fileIn);
         quest = (Quest) objectIn.readObject();
     } catch (FileNotFoundException e) {
         // Do nothing
     } catch (IOException e) {
         e.printStackTrace();
     } catch (ClassNotFoundException e) {
         e.printStackTrace();
     } finally {
         if (objectIn != null) {
             try {
                 objectIn.close();
             } catch (IOException e) {
                 Log.d(TAG, e.getMessage());
             }
         }
     }
     Toast.makeText(context, quest.getTitle(), Toast.LENGTH_LONG).show();

    questObserver.stopWatching();

Any help would be greatly appreciated.任何帮助将不胜感激。

' public abstract void onEvent (int event, String path) " - ' public abstract void onEvent (int event, String path) " -

This method is invoked on a special FileObserver thread.这个方法是在一个特殊的 FileObserver 线程上调用的。 It runs independently of any threads, so take care to use appropriate synchronization!它独立于任何线程运行,因此请注意使用适当的同步! Consider using post(Runnable) to shift event handling work to the main thread to avoid concurrency problems.考虑使用 post(Runnable) 将事件处理工作转移到主线程以避免并发问题。

http://developer.android.com/reference/android/os/FileObserver.html http://developer.android.com/reference/android/os/FileObserver.html

If you put the toast through a handler.post(new Runnable(){...}), that should work.如果您通过 handler.post(new Runnable(){...}) 将吐司放入,那应该可以。

Assuming your file doesn't (always) exist you should probably put your observer on the files folder, obtained like so:假设您的文件不(总是)存在,您可能应该将您的观察者放在 files 文件夹中,获取如下:

Context ctx = ...;
File filesFolder = ctx.getFilesDir();

Note that this will also ensure that the filesFolder directory will be created.请注意,这也将确保创建filesFolder目录。

Your observer will now be notified whenever a file is written, deleted or updated using for instance Context#.openFileOutput(..) - and you can filter in your FileObserver for the file name, in your example "quest".现在,每当使用Context#.openFileOutput(..)写入、删除或更新文件时,您的观察者都会收到通知 - 您可以在FileObserver过滤文件名,在您的示例“任务”中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM