3

At some point I want my Service to execute something after 2 seconds. So - I had prepared a designated Handler:

private Handler handler;
...
HandlerThread thread = new HandlerThread("LNT");
thread.start();
handler = new Handler(thread.getLooper());

and then I call

handle.postDelayed (new Runnable(){
    public void run(){
        doSomething();
}, (long)(2000));

sometimes soSomething() is being called after 2 seconds, but many times it take up to 15 seconds !!

What am I doing wrong? Is there another way to run something after a specific period of time

shadygoneinsane
  • 2,226
  • 1
  • 24
  • 47
DuduArbel
  • 1,100
  • 1
  • 12
  • 25
  • How do you determine when doSomething has run? – x-code May 29 '14 at 17:53
  • Did you gain any more insight into this problem? I'm seeing something similar. – Michael Alan Huff Jul 15 '15 at 16:15
  • @DuduArbel did you ever find a solution? I am having the same problem, only sometime on some phones. No matter how we delay execution, it ends up eventually being a handler with postdelayed and the problem persists. – Greg Ennis May 04 '17 at 21:14

3 Answers3

0

Probably, you need to execute your work on the Main Looper. Soooo, Why don't just do this:

new Handler(context.getMainLooper()).postDelayed(new Runnable(){
   @Override
   public void run(){
      doSomething();
   }
}, 2000);

Just test it and give me your feedback ;)

HatemTmi
  • 1,068
  • 9
  • 16
0

Your Handler can be busy with other tasks. Sometimes it's better to use an ExecutorService, like a Executors.newCachedThreadPool(). Or at any rate, that's how I just solved a similar issue.

Christine
  • 5,617
  • 4
  • 38
  • 61
-1

You can do it like this

private static final byte LNT = 1;

private Handler handler = new Handler()
{
    @Override
    public void handleMessage(Message msg)
    {
        switch (msg.what)
        {
            case LNT:

                Handler handler = null;
                handler = new Handler();
                handler.postDelayed(new Runnable()
                {
                    public void run()
                    {
                        doSomething();
                    }
                },2000);
            break;

            default:
            break;
        }
     }
}

And you call it with :

handler.sendEmptyMessage(LNT);
Farouk Touzi
  • 3,451
  • 2
  • 19
  • 25
  • Why would that be any different? – DuduArbel May 29 '14 at 16:55
  • The what value is basically an integer that allows the receiver to identify the messages it receives. We passed a Message object and we can check the public field what to figure out what the message is about. (msg.what). The idea is in this case you have only one type of messages and the Handler does understands that. So it does not need to check what kind of message it has received it just needs to receive a message and execute it after the time that you fix (here after 2 seconds. – Farouk Touzi May 29 '14 at 20:01
  • How does this solve the problem the original poster asked? Did you read the question? – Greg Ennis May 04 '17 at 21:14