Toast from FileObserver

I have a problem. I use FileObserverone that moves new files from scanned directories to another, previously specified directory. In my thoughts, a toast message should be indicated, which says: "The xy file was moved" if the observer is watching the directory, also if the applications are only in the background. But I did not work. It always tells me what exists RuntimeExceptionand that it is impossible to do without a call Looper.prepare().

05-11 13: 21: 28.484: WARN / System.err (3397): java.lang.RuntimeException: cannot create a handler inside a thread that has no name Looper.prepare ()

I also tried using a handler, but I also did not get it to work.

Does anyone have an idea? Thanks in advance.

Regards, Tobi

+3
source share
4 answers

Obviously, your FileObserver starts (or has) another thread. You cannot change the user interface from a thread other than the UI. Pass the handler to your FileObserver and send messages to it. Read about Handlers .

0
source

Before the Toast operation, add the following:

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

This will launch it in the user interface thread. Hope this helps.

+1
source

Toast? - .

0

:

// 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.

-1

All Articles