简体   繁体   English

如何从Android中的TimerTask用侦听器调用AyncTask?

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

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().". 我必须从TimerTask eack 1.5秒调用带有侦听器的异步任务。当我尝试执行此操作时,出现异常“ java.lang.RuntimeException:无法在未调用Looper.prepare( )。”。 Please help 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. 这里的BgTask id是AsyncTask类。

if you are in context of activity then this must work 如果你在活动的情况下,则它必须工作

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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