androidservicenotificationsfileobserver

Can i throw a Notification from a File Observer?


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    
    }
}

Solution

  • 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()