开发者

How to call AyncTask with listener from a TimerTask in android?

开发者 https://www.devze.com 2023-03-04 14:59 出处:网络
I have to call a Async Task with listener from the TimerTask eack 1.5 seconds.When I tried to do that I\'m getting an exception \"java.lang.RuntimeException: Can\'t create handler inside thread that h

I have to call a Async Task with listener from the TimerTask eack 1.5 seconds.When I tried to do that I'm getting an exception "java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare().". Please h开发者_运维知识库elp us to solve this issue.

Following is the piece of code I use..

     Timer t = new Timer();     
 TimerTask scanTask = new TimerTask()
 {
    @Override
    public void run()
        {
        new BgTask((BgTaskListener)this).execute("","currentState");
    }

};

t.schedule(scanTask, 1500, 1500);

Here BgTask id the AsyncTask class.


if you are in context of activity then this must work

    runOnUiThread(new Runnable()
    {           
        @Override
        public void run()
        {
            new BgTask((BgTaskListener)this).execute("","currentState");
        }
    });
0

精彩评论

暂无评论...
验证码 换一张
取 消