is my do loop code for java correct?

44 views Asked by At

is this correct? im modifying a source code from github : USB CHARGE COMMANDER when battery goes down from 20 percent it will charge when batter goes 80 it wont and countdown timer is for it to do this every 5 mins i set 20000 just for testing

    boolean startcountdown=true;
    do{
         new CountDownTimer(20000, 1000) {

        Intent intent  = _context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));   
        int    level   = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
        int    scale   = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
        int    percent = (level*100)/scale;

        public void onTick(long millisUntilFinished) {}

        public void onFinish() {
                if(percent <= 20){
                        _iIsCharging = 1;
                }
                else if (percent >=80){
                        _iIsCharging = 0;
                }
                else{
                        _iIsCharging = 1;
                }
            }
        }.start();
        }while(startcountdown);
1

There are 1 answers

2
Tyler Lazenby On BEST ANSWER

There is no way for this do/while loop to end, it's an infinite loop at the moment. The boolean "startcountdown" needs some way to eventually go to the value of FALSE. For example

if (_iIsCharging == 0) {
    startcountdown = false;
}