来自 FileObserver 的 Toast

发布于 2024-11-06 19:50:12 字数 404 浏览 1 评论 0原文

我有一个问题。我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一个人的旅程 2024-11-13 19:50:12

在 Toast 语句之前添加以下内容:

runOnUiThread(new Runnable() {
            public void run()
            {
                Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
            }
        });

这将使其在 UI 线程上运行。
希望这有帮助。

Before your Toast statement add the following :

runOnUiThread(new Runnable() {
            public void run()
            {
                Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
            }
        });

This will make it run on UI thread.
Hope this helps.

探春 2024-11-13 19:50:12

显然,您的 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.

动次打次papapa 2024-11-13 19:50:12

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.

握住我的手 2024-11-13 19:50:12

将以下代码放入您的类中:

// Need handler for callbacks to UI Threads
    // For background operations
    final Handler mHandler = new Handler();

    // Create Runnable for posting results
    final Runnable mUpdateResults = new Runnable() {
        public void run() {
            // Show the toast here.
        }
    };

并在您的 fileobserver 的线程调用中放置以下代码片段:

mHandler.post(mUpdateResults);

并且不要使用 getApplicationContext() 而是尝试 YourClassPhysicalName.java 作为Toast 的上下文。

Put the following code in your class:

// Need handler for callbacks to UI Threads
    // For background operations
    final Handler mHandler = new Handler();

    // Create Runnable for posting results
    final Runnable mUpdateResults = new Runnable() {
        public void run() {
            // Show the toast here.
        }
    };

and in your fileobserver's thread call place following fragment of code:

mHandler.post(mUpdateResults);

and don't use the getApplicationContext() instead try YourClassPhysicalName.java for the context of the Toast.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文