Chief Madog
Chief Madog

Reputation: 1507

cannot check state of boolean in timer

i'm using a wifi function that uses wifiManager.reconnect(); this returns me a Boolean state, that i want to monitor and call a callback function once it will be true, but when i do the timer it says that the variable will need to be final, and that will stop the whole purpose of why i'm doing this,

what am i doing wrong ?

here is the code:

isConnected = wifiManager.reconnect();

and then in my runnable i'm doing:

class MyTimerTask extends TimerTask {
            public void run() {
                // ERROR
                //CHANGE SOMETHING HERE
                Log.d ("myLogs", "Loop entered !!!");

                if (isConnected == true) {
                    myCallback.wifiConnectionEsteblishedCallable(true);
                }

                System.out.println("");
            }
        }



        MyTimerTask myTask = new MyTimerTask();
        Timer myTimer = new Timer();

        myTimer.schedule(myTask, 5000, 3000);

Upvotes: 0

Views: 66

Answers (2)

David
David

Reputation: 306

You could add a constructor to your task and pass the boolean flag to your TimeTask so it is not necessary to declare it final:

class MyTimerTask extends TimerTask {
   private boolean isConnected;

public MyTimerTask (boolean isConnected) {
      this.isConnected = isConnected;
}
            public void run() {
                // ERROR
                //CHANGE SOMETHING HERE
                Log.d ("myLogs", "Loop entered !!!");

                if (isConnected == true) {
                    myCallback.wifiConnectionEsteblishedCallable(true);
                }

                System.out.println("");
            }
}

Call it like this:

        MyTimerTask myTask = new MyTimerTask(isConnected);
        Timer myTimer = new Timer();

        myTimer.schedule(myTask, 5000, 3000);

Upvotes: 1

Mark Shen
Mark Shen

Reputation: 346

if isConnected is a global variable, everything would be fine.

Upvotes: 0

Related Questions