【问题标题】:How to stop a timer after certain number of times如何在一定次数后停止计时器
【发布时间】:2012-07-15 06:39:52
【问题描述】:

尝试使用 Timer 运行 4 次,每次间隔 10 秒。

我尝试用循环停止它,但它一直在崩溃。尝试使用带有三个参数的schedule(),但我不知道在哪里实现计数器变量。有什么想法吗?

final Handler handler = new Handler(); 
Timer timer2 = new Timer(); 

TimerTask testing = new TimerTask() {
    public void run() { 
        handler.post(new Runnable() {
            public void run() {
                Toast.makeText(MainActivity.this, "test",
                    Toast.LENGTH_SHORT).show();

            }
        });
    }
}; 

int DELAY = 10000;
for (int i = 0; i != 2 ;i++) {
    timer2.schedule(testing, DELAY);
    timer2.cancel();
    timer2.purge();
}

【问题讨论】:

    标签: android timer timertask


    【解决方案1】:
    private final static int DELAY = 10000;
    private final Handler handler = new Handler();
    private final Timer timer = new Timer();
    private final TimerTask task = new TimerTask() {
        private int counter = 0;
        public void run() {
            handler.post(new Runnable() {
                public void run() {
                    Toast.makeText(MainActivity.this, "test", Toast.LENGTH_SHORT).show();
                }
            });
            if(++counter == 4) {
                timer.cancel();
            }
        }
    };
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        timer.schedule(task, DELAY, DELAY);
    }
    

    【讨论】:

    • 谢谢,这个答案最有意义
    • 有趣的是他们没有在他们的界面中提供这个
    【解决方案2】:

    为什么不使用 AsyncTask 并在 while 循环中使用 Thread.sleep(10000) 和 publishProgress?这是它的样子:

    new AsyncTask<Void, Void, Void>() {
    
            @Override
            protected Void doInBackground(Void... params) {
    
                int i = 0;
                while(i < 4) {
                    Thread.sleep(10000);
                    //Publish because onProgressUpdate runs on the UIThread
                    publishProgress();
                    i++;
                }
    
                // TODO Auto-generated method stub
                return null;
            }
            @Override
            protected void onProgressUpdate(Void... values) {
                super.onProgressUpdate(values);
                //This is run on the UIThread and will actually Toast... Or update a View if you need it to!
                Toast.makeText(MainActivity.this, "test", Toast.LENGTH_SHORT).show();
            }
    
        }.execute();
    

    另外,对于长期重复性任务,请考虑使用AlarmManager...

    【讨论】:

      【解决方案3】:
      for(int i = 0 ;i<4 ; i++){
          Runnable  runnableforadd ;
          Handler handlerforadd ;
          handlerforadd = new Handler();
          runnableforadd  = new Runnable() {
              @Override
              public void run() {
                //Your Code Here
                  handlerforadd.postDelayed(runnableforadd, 10000);                         } 
          };
          handlerforadd.postDelayed(runnableforadd, i);
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-19
        • 1970-01-01
        • 1970-01-01
        • 2023-03-12
        相关资源
        最近更新 更多