Android - loop part of the code every 5 seconds

后端 未结 4 1983
醉酒成梦
醉酒成梦 2020-11-30 06:51

I would like to start repeating two lines of code every 5 seconds when I press the button START and end it, when I press the button STOP. I was trynig with a TimerTask and H

相关标签:
4条回答
  • 2020-11-30 07:29

    I don't have much more to add, other than to mention the differences between using Handler, CountDownTimer, and regular Timer. As britzl mentioned, the CountDownTimer uses a Handler internally, so that is equivalent to using the handler directly. A handler is used for running Ui stuff, for very short periods of time. An example would be setText for a text view. For computationally intensive tasks, handlers may cause a lag. A timer also can only run short tasks, but it is not necessarily only for UI stuff. For more complicated tasks, a new Thread should be used.

    0 讨论(0)
  • 2020-11-30 07:41

    You can use CountDownTimer as the following method:

    private CountDownTimer timer;
    
    timer = new CountDownTimer(5000, 20) {
    
        @Override
        public void onTick(long millisUntilFinished) {
    
        }
    
        @Override
        public void onFinish() {
            try{
                yourMethod();
            }catch(Exception e){
                Log.e("Error", "Error: " + e.toString());
            }
        }
    }.start();
    

    And then to call the timer again:

    public void yourMethod(){
        //do what you want
        timer.start();
    }
    

    To cancel the timer, you can call timer.cancel();

    Hope it helps!

    0 讨论(0)
  • 2020-11-30 07:51

    Using a CountDownTimer as in one of the other answers is one way to do it. Another would be to use a Handler and the postDelayed method:

    private boolean started = false;
    private Handler handler = new Handler();
    
    private Runnable runnable = new Runnable() {        
        @Override
        public void run() {
            final Random random = new Random();
            int i = random.nextInt(2 - 0 + 1) + 0;
            random_note.setImageResource(image[i]);
            if(started) {
                start();
            }
        }
    };
    
    public void stop() {
        started = false;
        handler.removeCallbacks(runnable);
    }
    
    public void start() {
        started = true;
        handler.postDelayed(runnable, 2000);        
    }
    

    Here's an example using a Timer and a TimerTask:

    private Timer timer;
    private TimerTask timerTask = new TimerTask() {
    
        @Override
        public void run() {
            final Random random = new Random();
            int i = random.nextInt(2 - 0 + 1) + 0;
            random_note.setImageResource(image[i]);
        }
    };
    
    public void start() {
        if(timer != null) {
            return;
        }
        timer = new Timer();
        timer.scheduleAtFixedRate(timerTask, 0, 2000);
    }
    
    public void stop() {
        timer.cancel();
        timer = null;
    }
    
    0 讨论(0)
  • 2020-11-30 07:52

    You can use RxJava2/RxAndroid2 and create an Observable that emits a message every second (or whatever you want), example with pseudo code:

    Disposable timer = Observable.interval(1000L, TimeUnit.MILLISECONDS)
                .timeInterval()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Timed<Long>>() {
                    @Override
                    public void accept(@NonNull Timed<Long> longTimed) throws Exception {               
                        //your code here.
                        Log.d(TAG, new DateTime());
                    }
                });
    

    When you want to stop it, you can simply call:

    timer.dispose();
    

    I find this code much more readable than the other options.

    0 讨论(0)
提交回复
热议问题