来自 FileObserver 的 Toast
我有一个问题。我正在使用 FileObserver,它将新文件从监视的目录移动到另一个以前指定的目录。在我看来,只要观察者观察目录,即使应用程序仅在后台运行,也应该显示一条 toast 消息,指出“文件 xy 已被移动”。但我没有让它发挥作用。 它总是告诉我,有一个 RuntimeException,并且如果不调用 Looper.prepare() 就无法完成。
05-11 13:21:28.484: 警告/系统错误(3397): java.lang.RuntimeException:不能 在线程内创建处理程序 没有调用Looper.prepare()
我也尝试过使用处理程序,但我也没有让它工作。
还有其他人有想法吗? 提前致谢。
最好的问候,托比
I have a problem. I'm using a FileObserver
, which moves new files from the watched directories to another, former specified directory. In my thoughts there should be shown a toast message that says 'File xy has been moved', as long as the observer watches the directory, also if the applications is only in the background. But I didn't get it working.
It always tells me, that there is a RuntimeException
, and that it cannot been done without calling Looper.prepare()
.
05-11 13:21:28.484:
WARN/System.err(3397):
java.lang.RuntimeException: Can't
create handler inside thread that has
not called Looper.prepare()
I tried the way with using an handler too, but I also didn't get it to work.
Has someone else an idea?
Thanks in advance.
Best regards, Tobi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 Toast 语句之前添加以下内容:
这将使其在 UI 线程上运行。
希望这有帮助。
Before your Toast statement add the following :
This will make it run on UI thread.
Hope this helps.
显然,您的 FileObserver 运行(或者是)另一个线程。您无法从非 UI 线程修改 UI。将 Handler 传递给 FileObserver 并从中发送消息。了解处理程序。
Obviously, your FileObserver runs(or is) another thread. You can not modify the UI from non-UI thread. Pass a Handler to your FileObserver and send messages from it. Read about Handlers.
Toast 消息的上下文使用什么?那必须有一种方法可以在屏幕上显示一些东西。
What are you using for the context of the Toast message? That will have to have a way to display something on the screen.
将以下代码放入您的类中:
并在您的 fileobserver 的线程调用中放置以下代码片段:
并且不要使用
getApplicationContext()
而是尝试YourClassPhysicalName.java
作为Toast 的上下文。Put the following code in your class:
and in your fileobserver's thread call place following fragment of code:
and don't use the
getApplicationContext()
instead tryYourClassPhysicalName.java
for the context of the Toast.