title: Android - Proper way to listen for variable change, and do something upon change?
body: <p>The app I'm making requires that a bit of code be executed whenever the value of a particular variable changes from 0 to 1.<br>
  The handler example below is the method I'm currently using to do this (I copied it from someone else).<br>
  I have a feeling it's not a proper method though because having just three of these handlers in my app causes the UI to be fairly unresponsive, and causes the device (a phone) to become quite hot.<br>
  As you can see, I've put 10ms delays in the handlers to try to deal with this.<br>
  Isn't there something more like <b>OnClickListener</b> that can listen at all times for a variable value change without putting such stress on the CPU?<br>
  I'm pretty new to Java and Android so a simple example would be very much appreciated.</p>
  
  <pre><code>  final Handler myHandler1 = new Handler();
    new Thread(new Runnable()
    {
    @Override
       public void run()
       {
          while (true)
          {
             try
             {
             Thread.sleep(10);
             myHandler1.post(new Runnable()
                {
                @Override
                   public void run() 
                   {
                      if (myVariable == 1)
                      {
                      myVariable = 0;
                      //do stuff
                      }   
                   }
                });
             } catch (Exception e) {}
          }
       }  
    }).start();
  </code></pre>