I have an Android app and I want to monitor a folder. I made a service(I want to monitor the folder non-stop, even if the user kill the app) and I put the folder's path in the extras of the Intent. In the service I have a FileObserver that should monitor my folder and trigger an event whenever a file is created inside the folder. The problem is that event is never triggered. Am I doing something wrong? Thanks!
@Override
public int onStartCommand(Intent intent, int flags, int startId){
String mainFolder = intent.getStringExtra("mainFolder");
Toast.makeText(getApplicationContext(), "Service start! Main folder is: " + mainFolder, Toast.LENGTH_LONG).show();
FileObserver observer = new FileObserver(mainFolder) {
@Override
public void onEvent(int event, String path) {
if (path == null) {
return;
}
if (FileObserver.CREATE == event) {
Toast.makeText(getApplicationContext(), "This file was creted: " + path, Toast.LENGTH_LONG).show();
}
}
};
observer.startWatching();
return START_REDELIVER_INTENT;
}