Android:TimerTask计划重复只被触发一次

好吧,这是我遇到的一个非常奇怪的问题,而且我很确定我在某个地方搞乱了,但我无法弄清楚在哪里.

我在想的是 –

>安排Timer每五秒钟执行一次TimerTask
> TimerTask依次执行AsyncTask(在这种情况下,简单地在返回AsyncTasks数量的静态计数之前休眠一秒钟).
>最后,在UI中更新上述计数.

当然,已经使用适当的Handler和Runnables将异步消息从其他线程发布到UI.

此代码仅执行一次.我希望它每5秒发射一次.这是代码.

注意:我不知道如何处理Looper.经过反复试验,我把它放在那里!

public class TimerAsyncMixActivity extends Activity {
    public static final String TAG = "TimerAsyncMix";
    static int executionCount = 0;
    Handler mHandler = new Handler();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        new Timer().schedule(new MyTimerTask(this), 0, 5000);
    }

    class MyAsyncTask extends AsyncTask<String, Void, Integer>{
        @Override
        protected Integer doInBackground(String... params) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return ++executionCount;
        }

        @Override
        protected void onPostExecute(Integer result) {

            mHandler.post(new UpdateUiThread(TimerAsyncMixActivity.this, result));
            super.onPostExecute(result);
        }
    }
}



class MyTimerTask extends TimerTask{
    private TimerAsyncMixActivity tma;

    public MyTimerTask(TimerAsyncMixActivity tma) {
        this.tma = tma;
    }

    @Override
    public void run() {
        Looper.prepare();
        Log.d(TimerAsyncMixActivity.TAG, "Timer task fired");
        tma.new MyAsyncTask().execute();
        Looper.loop();
        Looper.myLooper().quit();
    }
}

class UpdateUiThread implements Runnable{

    int displayCount;
    TimerAsyncMixActivity tma;
    public UpdateUiThread(TimerAsyncMixActivity tma, int i) {
        this.displayCount = i;
        this.tma = tma;
    }

    @Override
    public void run() {
        TextView tv = (TextView) tma.findViewById(R.id.tvDisplay);
        tv.setText("Execution count is : "+displayCount);
    }

谁能指出我做错了什么?

解决方法:

技术员,这就是我实施类似的事情.我不会声称这是最好的方式,但它对我有用,看起来并不太糟糕.

我的活动中有以下代码.我在活动开始时创建了一个异步任务,并将其停在onPause上. AsyncTask执行它需要做的任何事情,并更新onProgressUpdate()上的UI(它在UI线程上运行,因此不需要使用Handler).

private Task task;
@Override
protected void onPause() {
    task.stop();
    task = null;
}

@Override
protected void onResume() {
    task = new Task();
    task.execute();
}

private class Task extends AsyncTask<Void, String, Void> {

    private boolean running = true;
    @Override
    protected Void doInBackground(Void... params) {
        while( running ) {
            //fetch data from server;
            this.publishProgress("updated json");
            Thread.sleep(5000); // removed try/catch for readability
        }

        return null;
    }

    @Override
    protected void onProgressUpdate(String... values) {
        if( ! running ) {
            return; 
        }
        String json = values[0];
        //update views directly, as this is run on the UI thread. 
        //textView.setText(json);
    }

    public void stop() {
        running = false;
    }
}
上一篇:android – 获取主线程的消息队列和处理程序


下一篇:java修改class文件