我可以从文件观察器中抛出通知吗?

时间:2014-08-17 05:02:12

标签: android service notifications fileobserver

我正在构建这个应用程序,我需要监视一个文件夹,当一个文件被修改(创建,删除,无关紧要)我必须通知用户。我尝试了很多方法,但由于FileObserver是一个抽象类,因此无法达到运行通知的目的。

重要的是要注意FileObserver正在通过服务运行,我正在监视来自另一个程序的纯文本,该程序将文件夹与我的应用程序进行同步。

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

1 个答案:

答案 0 :(得分:0)

您需要发送通知的只是一个Context引用,您可以从中获取NotificationManager实例并将其传递给您的Notification。您最好的选择可能是在构造函数中传递应用程序上下文并将其保留为字段。

Context.getSystemService()

Context.getApplicationContext()