ben
ben

Reputation: 393

looper.prepare question

i am trying to loop a toast inside a timer but the toast doesn't show

the log in logcat shows that cannot create handler inside thread that has not called looper.prepare() i am not sure what it means

    int initialDelay = 10000;
    int period = 10000;
    final Context context = getApplicationContext();
    TimerTask task = new TimerTask() 
    {
        public void run() 
        {               
            try
            {
                if (a != "")
                {   
                      Toast toast = Toast.makeText(context, "Alert Deleted!", Toast.LENGTH_SHORT);
                      toast.show();
                }
            }
            catch (Exception e)
            {

            }
        }
    };
    timer.scheduleAtFixedRate(task, initialDelay, period);

what my application does is that every 10 sec it would check if a certain variable is empty. if it is empty then it will show a toast.

i have no problem doing this in a service class but when i try to implement this into

 public void onCreate(Bundle savedInstanceState)

i get this error

Upvotes: 1

Views: 7718

Answers (2)

user1530779
user1530779

Reputation: 509

You can show this toast in alternative ways also

class LooperThread extends Thread {
      public Handler mHandler;

      @Override
      public void run() {
          Looper.prepare();

          mHandler = new Handler() {
              @Override
              public void handleMessage(Message msg) {
                  // process incoming messages here
              }
          };

          Looper.loop();
      }
  }

Now as you see this handler is created in normal thread so if you try n send any message from it , it will throw an exception so by bounding it with Looper.prepare() and Looper.loop() you can make any statements executed within it on UI thread

Another Example

Looper allows tasks to be executed sequentially on a single thread. And handler defines those tasks that we need to executed. It is a typical scenario that I am trying to illustrate in example:

class SampleLooper {
@Override
public void run() {
  try {
    // preparing a looper on current thread     
    // the current thread is being detected implicitly
    Looper.prepare();

    // now, the handler will automatically bind to the
    // Looper that is attached to the current thread
    // You don't need to specify the Looper explicitly
    handler = new Handler();

    // After the following line the thread will start
    // running the message loop and will not normally
    // exit the loop unless a problem happens or you
    // quit() the looper (see below)
    Looper.loop();
  } catch (Throwable t) {
    Log.e(TAG, "halted due to an error", t);
  } 
}
}

Now we can use the handler in some other threads(say ui thread) to post the task on Looper to execute.

handler.post(new Runnable()
{
public void run() {`enter code here`
//This will be executed on thread using Looper.`enter code here`
    }
});

On UI thread we have an implicit Looper that allow us to handle the messages on ui thread.

Upvotes: 0

eyespyus
eyespyus

Reputation: 1576

You're calling it from a worker thread. You need to call Toast.makeText() (and most other functions dealing with the UI) from within the main thread. You could use a handler, for example.

see this answer....

Can't create handler inside thread that has not called Looper.prepare()

Upvotes: 2

Related Questions