将广播消息从类发送到Android中的活动(Send Broadcast message from a class to an Activity in Android)

编程入门 行业动态 更新时间:2024-10-27 19:28:51
将广播消息从类发送到Android中的活动(Send Broadcast message from a class to an Activity in Android)

我正在编写一个应用程序,它需要监视正在创建的文件的文件夹,然后根据创建的文件的文件名执行操作。

我已经将广播接收器用于来自电池和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.

更多推荐

本文发布于:2023-08-04 20:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1421421.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送到   消息   Android   Send   Activity

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!