Search code examples
javaandroidtimercountdownflashlight

CountDownTimer variable


I have a major problem about changing the value from a constructor

This is an app for flashlights. It will connect to a site to check the interval value (ex. 500 ms) and it will store it to an variable named frum_timer every 2 seconds.

Here's a object for that. (also it will update a boolean if there's a new value)

FlashActivity = new CountDownTimer(40000, frum_timer) {

    @Override
    public void onTick(long millisUntilFinished) {
        stats_of_run = true;
        if (bool) {
            final_form.setText("\\Flash ON//");
            // CAMERA INSTRUCTIONS FOR OPENING FLASH
            bool = false;
            frumtimer_stats.setText("Speed:" + frum_timer);
        } else {
            stats_of_run = true;
            final_form.setText("->Flash OFF-<");
            // CAMERA INSTRUCTIONS FOR CLOSING FLASH
            bool = true;
        }
    }

    @Override
    public void onFinish() {
    }

}.start();  

I made another CountDownTimer object with a refresh rate of 200 seconds to make sure the frum_timer from FlashActivity object will take change.

and using this

frumtimer_stats.setText("Speed:" + frum_timer);

in FlashActivity for displaying the variable.

However, after the variable frum_timer is changed, the FlashLight keep going again and again at the same old speed even if a made an FlashActivity.cancel() followed by FlashActivity.start()

Can someone give me some help?

Full link of code: https://mycodestock.com/public/snippet/14395

Summary: 1.You start the app. 2. Refresh countdown starts in 2 sec 3. When there is a frum_timer the FlashActivity will start. After 2 sec, if there is another value stored on frum_timer, the actual FlashActivity will be canceled and will start a new one. 4.The problem is the new FlashActivity start but with the old frum_timer


Solution

  • I used:

     public void startProgress(View view) {
            // do something long
            Runnable runnable = new Runnable() {
              @Override
              public void run() {
                for (int i = 0; i <= 20; i++) {
                  final int value = i;
                   doFakeWork();
    
    
                  progress.post(new Runnable() {
                    @Override
                    public void run() {
                     // text.setText("Updating");
                        progress.setMax(20);
                      progress.setProgress(value);
                    }
                  });
                }
              }
            };
            new Thread(runnable).start();
          }
    
    
      private void doFakeWork() {
            try {
              Thread.sleep(800);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
          }