简体   繁体   中英

Android - listen to changes in a folder

I have a Bluetooth application, which will list out all of the files in the bluetooth folder on my phone. So far I have only done it this way:

public class GetCaseInformation {

int numberOfFiles;
String pathToFolder;
File folder;

public GetCaseInformation(String pathToFolder) {

    this.pathToFolder = pathToFolder; 
    folder = new File(pathToFolder);
}


public int getCasesFromFolder() {

    return folder.list().length; 

}

}

A very simple way to check how many files I have in my folder. The method getCasesFromFolder() is called in my onCreate method, so this will just be updated every time the user goes to the home screen. Is it possible to listen for changes in a spesific folder? So I dynamicaly can update the count of files in this folder?

You can try FileObserver :

Monitors files (using inotify) to fire an event after files are accessed or changed by by any process on the device (including this one). FileObserver is an abstract class; subclasses must implement the event handler onEvent(int, String).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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