Android listening for files and directory dynamics

Source: Internet
Author: User

The Fileobserver class under Android.os is a listener that listens for file access, creation, modification, deletion, and movement, based on Linux inotify. Fileobserver is an abstract class that must be inherited before it can be used. Each Fileobserver object listens to a separate file or folder, and if a folder is monitored, all files and cascading subdirectories under the folder trigger a listener event.

The types of events that can be monitored are as follows:

Access, where files are accessed

MODIFY, File is modified

ATTRIB, file attributes are modified, such as chmod, chown, touch, etc.

Close_write, writable file is close

Close_nowrite, cannot write file is close

Open, File is open

Moved_from, files are removed, such as MV

Moved_to, files are moved, such as MV, CP

Create, creating a new file

Delete, the file is deleted, such as RM

Delete_self, since deletion, that is, an executable file that deletes itself at execution time

Move_self, self-moving, that is, an executable file that moves itself at execution time

Close, file is closed, equivalent to (In_close_write | In_close_nowrite)

All_events, including all the events above

These are the static common properties of the Fileobserver. Here is an example to illustrate its usage. For example, I want to listen to the SD card directory creation event, create a new class Sdcardlistener inherited Fileobserver:

Import Android.os.FileObserver;

Import Android.util.Log;

/**

* Directory creation listener on SD card

* @author Way

*

*/

public class Sdcardlistener extends Fileobserver {

Public Sdcardlistener (String path) {

/*

* This construction method is to listen for all events by default, and if you use the super (String,int) construct method,

* The int parameter is the type of event to listen for.

*/

Super (path);

}

@Override

public void OnEvent (int event, String path) {

Switch (event) {

Case Fileobserver.all_events:

LOG.D ("All", "path:" + path);

Break

Case Fileobserver.create:

LOG.D ("Create", "Path:" + path);

Break

}

}

}

OnEvent is a callback, the system will trigger this event after hearing the event, parameter event is the event type mentioned above, according to the type, we can do the corresponding processing, parameter path is the directory that triggers the event.

Then use it:

Sdcardlistener listener = new Sdcardlistener ("/sdcard");

Start listening.

Listener.startwatching ();

/*

* Doing some work here, like creating a directory or something.

*/

Finally stop listening.

Listener.stopwatching ();

If you want to do more in the onevent, it is best to use a thread to avoid the subsequent events because of blocking.

Important:

The Fileobserver object must maintain a reference to ensure that it is not reclaimed by the garbage collector, otherwise the event will not be triggered.

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.