Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toast from FileObserver

Tags:

android

toast

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

like image 953
Tobi N Avatar asked Nov 30 '25 13:11

Tobi N


1 Answers

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.

like image 141
Deep Shah Avatar answered Dec 02 '25 03:12

Deep Shah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!