The use of Android Fileobserver and the guide to avoiding pits
Source: Internet
Author: User
<span id="Label3"></p><p><p>Yesterday because the project needs, need to do a damo, use the file monitoring function, so the use of Fileobserver (file observer) This class, the result is not the pit, the end of work has been exhausted, and the spirit of the bug bared teeth must report, here to write down this account, I also hope that we will not waste time on This.</p></p>Features of the Fileobserver<p><p>therefore, the name incredibles, is to listen to the file of the event, according to the official documents, there are several events response, corresponding to the different event Constants.</p></p> <table> <thead> <tr> <th align="left">constant Name</th> <th align="left">corresponding Events</th> </tr> </thead> <tbody> <tr> <td align="left">ACCESS</td> <td align="left">Actions to read files after opening a file</td> </tr> <tr> <td align="left">All_events</td> <td align="left">All events in the event list</td> </tr> <tr> <td align="left">ATTRIB</td> <td align="left">Operation not specified</td> </tr> <tr> <td align="left">Close_nowrite</td> <td align="left">read-only files are closed</td> </tr> <tr> <td align="left">Close_write</td> <td align="left">Read and write files are edited and closed</td> </tr> <tr> <td align="left">CREATE</td> <td align="left">File created by creation</td> </tr> <tr> <td align="left">DELETE</td> <td align="left">File is deleted</td> </tr> <tr> <td align="left">Delete_self</td> <td align="left">Self-deletion Events</td> </tr> <tr> <td align="left">MODIFY</td> <td align="left">File is modified</td> </tr> <tr> <td align="left">Moved_from</td> <td align="left">Move Out Event</td> </tr> <tr> <td align="left">Moved_to</td> <td align="left">Move in Events</td> </tr> <tr> <td align="left">Move_self</td> <td align="left">Self-moving Events</td> </tr> <tr> <td align="left">OPEN</td> <td align="left">File is opened</td> </tr> </tbody> </table><p><p>These constants are in the Fileobserver class, so they can be called by fileobserver.xxx.</p></p><p><p>Fileobserver can listen to two types of files: one is a single file and the other is a file directory. It is important to note that there is no recursive subdirectory when listening to the file directory, so either make sure that there are no subdirectories under the listening file, or do a special operation and manually listen to each subdirectory recursively.</p></p>Fileobserver usage 1.FileObserver is an abstract class that we use to implement a class to inherit Fileobserver. 2. Writing the construction method<p><p>There are two ways to construct a method:</p></p>The first is to pass only the path parameter<pre><pre><code>public FileObserver (String path)</code></pre></pre><p><p>In fact, when using this method, in order to prevent the occurrence of nested calls to produce a dead loop, we can do a bit of optimization when implementing the Method.</p></p><pre><pre><code>public FileObserver (String path) { super( path, FileObserver.CREATE | FileObserver.DELETE );}</code></pre></pre><p><p>As you can see, when we inherit the Super method, we call another construction parameter of the parent class, and in the second parameter we pass in the event we need to listen to, so that we can filter out unwanted events and reduce the probability of a dead loop.</p></p>Second pass path and event to listen on<pre><pre><code>public FileObserver (String path, int mask)</code></pre></pre><p><p>We have seen the use of this method in the optimization of the first method, which can be constructed using this method, which is less coupled than the first method.</p></p>3. Implement the OnEvent (int event, String Path) method<p><p>As I said earlier, Fileobserver is an abstract class, and this onevent method is its abstract method, which needs to be implemented in Subclasses.</p></p><pre><pre><code>@Overridepublic void onEvent(int event, String path) { /*event的值是与0x40000000进行或运算后的值,所以在case之前需要先和FileObserver.ALL_EVENTS进行与运算*/ int e = event & FileObserver.ALL_EVENTS; switch (e) { case FileObserver.CREATE: /*do something*/ break; }}</code></pre></pre>4. Turn on and off monitoring<p><p>The fileobserver provides a way to turn on and off the listener, just call it directly.</p></p><pre><pre><code>startWatching(); //开启监听stopWatching(); //结束监听</code></pre></pre><p><p>One thing to be aware of is that as a listener the fileobserver needs to keep a reference to prevent it from being killed by the Java GC mechanism, so it's best to work with a persistent reference binding, typically using service for Binding.</p></p>5. Permissions Issues<p><p>All of the online tutorials on using fileobserver do not mention permission requests, but I used it yesterday because of a problem with permissions that have not been monitored. and fileobserver use of the authority did not apply when not the usual error hint, just silently strike, personally think is the biggest pit.</p></p><p><p>Add the following permissions to the manifest file:</p></p><pre><pre><code><!--往sdcard中写入数据的权限 --><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission><!--在sdcard中创建/删除文件的权限 --><uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission></code></pre></pre><p><p>Suggestions are encountered in how to listen to the time and then add permissions, after all, permission to apply too many users are very disgusted.</p></p>Summarize<p><p>Through the above introduction can see, Fileobserver pit really is super many, is really beach risk stone more, need patience and cautious to try to Avoid.</p></p>Citation declarations<p><p>Android file or folder content Change listener (fileobserver)--mayingcai1987 blog.</p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>The use of Android Fileobserver and the guide to avoiding pits</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service