Android FileObserver usage and avoidance guide, androidobserver
Yesterday, a damo was required for the project and the file listening function was used. Therefore, the FileObserver (File observer) class was used, and the result would not work, I was exhausted after work yesterday. In the spirit of being unable to report bugs, I would like to write down this bill and hope that you will not waste time on it in the future.
Functions of FileObserver
The event is the event of the listener file. According to the official documents, there are several event responses corresponding to different event constants.
| Constant name |
Corresponding event |
| ACCESS |
Read a file after opening the file |
| ALL_EVENTS |
All events in the event list |
| ATTRIB |
Unknown operation |
| CLOSE_NOWRITE |
The read-only file is disabled. |
| CLOSE_WRITE |
The read/write file is edited and disabled. |
| CREATE |
File Created and created |
| DELETE |
File deleted |
| DELETE_SELF |
Auto-delete event |
| MODIFY |
File Modified |
| MOVED_FROM |
Removal event |
| MOVED_TO |
Migration event |
| MOVE_SELF |
Self-moving event |
| OPEN |
File opened |
These constants are all in the FileObserver class, so they can be called through FileObserver. XXX.
FileObserver can listen to two types of files: one is a single file and the other is a file directory. Note that there is a non-Recursive Sub-directory problem when listening to the file directory. Therefore, either ensure that there are no sub-directories under the listening file, or perform special operations to manually listen to each sub-directory recursively.
FileObserver usage 1. FileObserver is an abstract class. When using FileObserver, we need to implement a class to inherit FileObserver. 2. Compile the constructor
The constructor can be written in either of the following ways:
First, only pass path parameters
public FileObserver (String path)
In fact, when using this method, we can perform some optimization when implementing the method to prevent nested calls from generating an endless loop.
public FileObserver (String path) { super( path, FileObserver.CREATE | FileObserver.DELETE );}
We can see that when we inherit the super method, another constructor of the parent class is called. In the second parameter, the events we need to listen to are passed in, in this way, you can filter out unnecessary events and reduce the probability of an endless loop.
Second, upload path and event to be monitored
public FileObserver (String path, int mask)
We have seen the usage of this method in the optimization of the first method. This method can also be used for construction. Compared with the first method, this method has lower coupling.
3. Implement the onEvent (int event, String path) Method
As mentioned above, FileObserver is an abstract class, And the onEvent method is its abstract method, which must be implemented in the subclass.
@ Overridepublic void onEvent (int event, String path) {/* the value of the event is the value after the or operation with 0x40000000. Therefore, you need to contact FileObserver before case. ALL_EVENTS and Operations */int e = event & FileObserver. ALL_EVENTS; switch (e) {case FileObserver. CREATE:/* do something */break ;}}
4. Enable and disable listeners
In FileObserver, you can enable or disable a listener by directly calling it.
StartWatching (); // enable listening stopWatching (); // end listening
It should be noted that the FileObserver, as the listener, must always maintain a reference to prevent being killed by the java GC mechanism. Therefore, it is best to bind the file to a persistent reference, generally, we use services for binding.
5. Permission issues
I did not mention the permission application in all the tutorials on using FileObserver on the Internet. However, I used it yesterday because of permission issues that caused the failure to listen. In addition, when FileObserver does not apply for permissions, it does not mean a regular error message, but it just strikes silently. I personally think it is the biggest pitfall.
Add the following permissions to the configuration file:
<! -- Write data to sdcard --> <uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"> </uses-permission> <! -- Create/delete file permissions in sdcard --> <uses-permission android: name = "android. permission. MOUNT_UNMOUNT_FILESYSTEMS"> </uses-permission>
We recommend that you add permissions when you fail to listen to the requests. After all, many users are disgusted with permission applications.
Summary
From the above introduction, we can see that there are so many pitfalls in FileObserver, and there are so many dangerous rocks in it. We need to be patient and careful to avoid them as much as possible.
Reference Declaration
For Android files or folders, change the listener (FileObserver)-mayingcai1987 blog.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.