0

I'm building this app in which I need to monitor a folder, when a file is modified (creatd, deleted, doesn´t matter) I have to notify the user. I have tried many ways and I can't reach the goal of running a notification because FileObserver is an abstract class.

It is important to notice that FileObserver is running trough a service, I'm monitoring plain texts that comes from another program that sychronices a folder with my app.

public class MyFileObserver extends FileObserver{

    public String absolutePath;
    public MyFileObserver(String path) 
    {
        super(path, FileObserver.ALL_EVENTS);
        absolutePath = path;
        Log.v("","Now watching");
    }
    @Override
    public void onEvent(int event, String path) {
        if (path == null) {
            return;
        }
        if ((FileObserver.CREATE & event)!=0) 
        {
            FileAccessLogStatic.accessLogMsg +=absolutePath+"/"+path+"is created\n";

            // Need to run a notification here

         }

         //here comes all the other methods    
    }
}
Squonk
  • 48,735
  • 19
  • 103
  • 135

1 Answers1

0

All you need to send a notification is a Context reference, from which you can get the NotificationManager instance and pass it your Notification. Your best bet would probably be to pass in the application context in the constructor and to keep it as a field.

Context.getSystemService()

Context.getApplicationContext()

Autumn
  • 91
  • 1
  • There's no need to use the application context - the OP is using the `FileObserver` with a `Service` which is, in itself, a `Context`. – Squonk Aug 17 '14 at 06:52
  • Thanks for your answer Autum. I've tried that and now i'm trying again, i got a problem when i get to the line: NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); NOTIFICATION_SERVICE cannot be resolved to a variable, and I don't know how to solve that error. – Luis Correa Vélez Aug 17 '14 at 19:41
  • Try `Context.NOTIFICATION_SERVICE`. – Autumn Aug 18 '14 at 00:00