我正在编写一个应用程序,它需要监视正在创建的文件的文件夹,然后根据创建的文件的文件名执行操作。
我已经将广播接收器用于来自电池和USB连接的其他通知,因此能够从实现fileObserver的类发送广播会很好。
在Main活动中,我为fileObserver实现了一个子类,但我无法弄清楚如何在一个事件中我可以通知MAin Activity创建了一个文件。
这是子类
class FileListener extends FileObserver { private String mAbsolutePath; public FileListener(String path){ super(path); mAbsolutePath = path; Log.d("FileObserver",path); } @Override public void onEvent(int event, String path){ switch(event){ case FileObserver.CREATE: Intent i = new Intent("CREATED"); context.sendBroadcast(i); break; case FileObserver.DELETE: Log.d("FileObserver", "DELETE"); break; case FileObserver.DELETE_SELF: Log.d("FileObserver", "DELETE_SELF"); break; case FileObserver.MODIFY: Log.d("FileObserver", "MODIFY"); break; case FileObserver.MOVED_FROM: Log.d("FileObserver", "MOVED_FROM"); break; case FileObserver.MOVED_TO: Log.d("FileObserver", "MOVED_TO"); break; case FileObserver.MOVE_SELF: Log.d("FileObserver", "MOVE_SELF"); break; } }}
我不能在类中使用context.sendBroadcast,因为它没有上下文。 这一切都非常令人困惑。
谢谢
I am writing an application that will need to monitor a folder for files being created and then take actions based on the file name of the file that was created.
I am already using a Broadcast Receiver for other notifications coming from the battery and the usb connection so it would be nice to be able to send a broadcast from the class that is implementing a fileObserver.
In the Main activity I implemented a sub class for the fileObserver but I cannot figure out how, upon an event I can notify the MAin Activity that a file was created.
Here is the Sub Class
class FileListener extends FileObserver { private String mAbsolutePath; public FileListener(String path){ super(path); mAbsolutePath = path; Log.d("FileObserver",path); } @Override public void onEvent(int event, String path){ switch(event){ case FileObserver.CREATE: Intent i = new Intent("CREATED"); context.sendBroadcast(i); break; case FileObserver.DELETE: Log.d("FileObserver", "DELETE"); break; case FileObserver.DELETE_SELF: Log.d("FileObserver", "DELETE_SELF"); break; case FileObserver.MODIFY: Log.d("FileObserver", "MODIFY"); break; case FileObserver.MOVED_FROM: Log.d("FileObserver", "MOVED_FROM"); break; case FileObserver.MOVED_TO: Log.d("FileObserver", "MOVED_TO"); break; case FileObserver.MOVE_SELF: Log.d("FileObserver", "MOVE_SELF"); break; } }}
I can't use context.sendBroadcast in the class because it has no context. This is all very confusing.
Thanks
最满意答案
您需要做的就是将构造函数传递给您的类,如下所示:
class FileListener extends FileObserver { private String mAbsolutePath; private Context mContext; public FileListener(String path, Context context){ super(path); mAbsolutePath = path; mContext = context; Log.d("FileObserver",path); } //use context in your file.然后,您可以将类称为: new FileListener(path, getApplicationContext()来自您的活动或片段的new FileListener(path, getApplicationContext() 。
All you need to do is pass your constructor into you class like so:
class FileListener extends FileObserver { private String mAbsolutePath; private Context mContext; public FileListener(String path, Context context){ super(path); mAbsolutePath = path; mContext = context; Log.d("FileObserver",path); } //use context in your file.You can then call you class like: new FileListener(path, getApplicationContext() from your activity or fragment.
更多推荐
发布评论