本文介绍了在TimerTask的开始的AsyncTask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我要进行的AsyncTask当倒计时完成一个计时器。如果我把它执行的处理程序会循环,并启动了很多次。如果我不把它放在一个处理程序我得到以下崩溃:不能创建处理程序内螺纹已经不叫尺蠖。prepare()

I have a timer that I want to start an AsyncTask when the countdown is done. If I put the execution of it in a handler it loops it and starts it many times. And if I dont put it in a Handler I get the following crash:can't create handler inside thread that has not called looper.prepare()

timer.schedule(new ListUpdate(), helper.nextListUpdate.get(0));

class ListUpdate extends TimerTask {
    private Handler mHandler = new Handler(Looper.getMainLooper());
    public void run() {
        mHandler.post(new Runnable() {
            public void run() {
                AsyncTask<Integer, Void, Boolean> task = new updateList();
                task.execute();
            }
        });
    }
}

如何,我可以解决这个问题有什么建议?

Any suggestions of how I can solve this?

推荐答案

通过添加一个TimerTask之外的处理器这是我从一个TimerTask打电话,我可以使它的工作!

By adding a handler outside of the TimerTask which I call from the TimerTask I could make it work!

final Handler handler = new Handler() {
    public void handleMessage(Message msg) {
        RelativeLayout rl_header = (RelativeLayout)findViewById(R.id.rl_header);
        Desktop desktop = helper.getDesktop();
        try {
            desktop.inflate(ll, rl_header, banners, DesktopApp.this);
            Collections.sort(helper.nextListUpdate);
            helper.nextListUpdate.remove(0);
            timer = new Timer();
            if (helper.nextListUpdate.size() > 0) timer.schedule(new ListUpdate(), helper.nextListUpdate.get(0));
        } catch (Exception e) {
            e.printStackTrace();
        }
     }

};

class ListUpdate extends TimerTask {
    public void run() {
        DesktopApp.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                handler.sendEmptyMessage(0);
            }
        });
    }
}

这篇关于在TimerTask的开始的AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 01:44