0

Android application have read/write permission for /data/anr/traces.txt. But still FileObserver does not seem to work for it.

What else is required for FileObserver to work? It works fine for sdcard file.

Code:

mFileObserver = new FileObserver("/data/anr/traces.txt") { // set up a file observer to
                @Override
                public void onEvent(int event, String file) {
                    if(event == FileObserver.CLOSE_WRITE)
                    {
                        Log.e("TestApp", "ANR has occured");
                    }
                }
            };
            mFileObserver.startWatching();
AndRSoid
  • 1,777
  • 2
  • 13
  • 25

2 Answers2

1

Found solution to it therefore answering my own question as it may help others.

Looks like using complete file path "/data/anr/traces.txt" is not working. But using path to "/data/anr" is working ok.

Still not sure why complete path doesn't work.

AndRSoid
  • 1,777
  • 2
  • 13
  • 25
  • As a guess, the file is getting replaced, and you don't have an observer registered on the replacement. If you logged events other than CLOSE_WRITE you might see some evidence of the replacement operation, but you seem to already have a solution. – Chris Stratton Jul 25 '14 at 19:53
0

Before calling mFileObserver.startWatching(), you should make sure the file exists. Or no events will be notified.

  • This should be a comment (once you have [enough reputation](http://stackoverflow.com/help/privileges) to add one) instead of an answer. Please refer to the [Help Section](http://stackoverflow.com/help/whats-reputation) to learn how to earn reputation and therefore be able to comment. – mathielo Sep 09 '14 at 16:31