0

I need to track removed images and videos from the whole file system all the time (even if my UI app is not running) so directly after startup of the handset until it's turned off. For this purpose I'm using a service and FileObserver. Unfortunately the code attached below doesn't work (I don't get with debugger to the method

public void onEvent(int event, String file)

although I made some changes to the FS... The Service gets started and obviously onStarted() method is executed...

please help

public class DataChangeListener extends Service {

    private static Context context;

    @Override
    public IBinder onBind(Intent intent) {
        context = this;
        return null;
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        context = this;

        FileObserver observer = new FileObserver("/") {

            @Override
            public void onEvent(int event, String file) {
                System.out.println();
            }
        };
        observer.startWatching();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }
}
basta
  • 145
  • 1
  • 8

1 Answers1

2

I need to track removed images and videos from the whole file system all the time

That will not be possible, unless you create a modified version of the OS, as you will not be able to keep a service running all of the time. The user can get rid of your service whenever the user wants, for example.

Unfortunately the code attached below doesn't work

First, you have no read access to /, which may interfere with FileObserver operation.

Second, you missed a highlighted paragraph in the FileObserver documentation:

Warning: If a FileObserver is garbage collected, it will stop sending events. To ensure you keep receiving events, you must keep a reference to the FileObserver instance from some other live object.

You are allowing the FileObserver to be garbage-collected.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 2
    service can be made always alive by returning START_STICKY from onStartCommand().In this case, even if I kill it in DDMS, it is immediately re-started by a system. If user explicitly stop the service that's another story. I think the problem here is lack of permissions though. – avepr Nov 23 '12 at 00:42