简体   繁体   中英

android sendmessagedelayed takes too long

I am doing a repetitive task using the UI thread's message queue. That is, in my mainActivity I am using the following block:

private Handler mHandler = new Handler() {

    @Override
    public void handleMessage(Message msg) {
    }
}

But, when I send a message from inside a callback by using

mHandler.sendEmptyMessageDelayed(what, 5);

my delay is about 20ms instead of the expected 5ms. Does anyone know why this sort of thing can happen? Is it possible to increase thread priority to solve it?

You can use an alternate

private Handler mHandler = new Handler() {

    @Override
    public void handleMessage(Message msg) {
      //.......
    }
}

Handler handler = new Handler();
handler.postDelayed(new Runnable() {

    @Override
    public void run() {
        mHandler.sendMessage("");
    }
}, 5000);

May be it helpful for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM