安卓:TimerTask的预定重复被解雇只有一次 [英] Android: TimerTask scheduled for repetition getting fired only once

查看:133
本文介绍了安卓:TimerTask的预定重复被解雇只有一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定这是一个很奇怪的问题,我有,我就是pretty的肯定,我弄乱的地方,但我不能完全弄清楚的地方。

Ok this is a very weird problem I am having, and I'm pretty sure that I am messing up somewhere, but I can't quite figure out where.

我想做的 -

  • 安排定时器执行的TimerTask 每五秒钟
  • 的TimerTask 又返回静态之前执行的的AsyncTask (在这种情况下,简单的休眠第二算上AsyncTasks的数目)。
  • 最后,上述的计数在UI更新。
  • Schedule a Timer to execute a TimerTask every five seconds
  • The TimerTask in turn executes an AsyncTask (which in this case simple sleeps for a second before returning the static count of the number of AsyncTasks).
  • Finally, the aforementioned count is updated in the UI.

当然,适当的处理程序的Runnable 取值已经被用来发布从其他线程异步消息到用户界面。

And of course, the appropriate Handlers and Runnables have been used to post asynchronous messages from other threads to the UI.

这code只执行一次。我希望它激发每5秒。这里的code。

This code executes only once. I expect it to fire every 5 seconds. Here's the code.

注意:我不知道该怎么办了尺蠖。我把它放在那里经过反复试验!

Note: I had no idea what to do with the Looper. I put it there after trial and error!

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);
    }

任何人都可以指向我什么,我做错了什么?

Can anyone point me to what I'm doing wrong?

推荐答案

技术人员,我这是怎么实现类似的事情。我不会声称这是最好的方式,但它很适合我,不看太糟糕了。

techie, this is how I implemented similar things. I'm won't claim that this is the best way, but it has worked for me and doesn't look too bad.

我有以下的code。在我的活动。我创建了一个异步任务时活动开始,我停止它的onPause。该AsyncTask的做什么,它需要做,并更新onProgressUpdate()用户界面(这是运行在UI线程上,所以没有必要使用一个处理器)。

I have the following code in my activity. I create an async task when the activity starts and I stop it onPause. The AsyncTask does whatever it needs to do, and updates the UI on onProgressUpdate() (which is run on the UI thread, so there's no need to use a 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;
    }
}

这篇关于安卓:TimerTask的预定重复被解雇只有一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆