title: How to do something repeatedly while a button is pressed?
body: <p>I'm developing a TV-Remote Simulator app in Android using a specific middleware (not of importance). </p>
  
  <p>In the case of Volume Buttons (Volume+ and Volume-), what I'm trying to do is to send the "Volume Up" signal repeatedly while its button is pressed.</p>
  
  <p>That's what I tried at last (code for one of the buttons, the other must be identical except for the names):</p>
  
  <ol>
  <li><p>Declared a boolean variable</p>
  
  <pre><code>boolean pressedUp = false;
  </code></pre></li>
  <li><p>Declared a inner class with the following AsyncTask:</p>
  
  <pre><code>class SendVolumeUpTask extends AsyncTask&lt;Void, Void, Void&gt; {
  
      @Override
      protected Void doInBackground(Void... arg0) {
          while(pressedUp) {
              SendVolumeUpSignal();
      }
      return null;
  }
  </code></pre>
  
  <p>}</p></li>
  <li><p>Add the listener to the button:</p>
  
  <pre><code>final Button buttonVolumeUp = (Button) findViewById(R.id.volup);
  buttonVolumeUp.setOnTouchListener(new View.OnTouchListener() {
  
      @Override
      public boolean onTouch(View v, MotionEvent event) {
          switch (event.getAction()) {
  
          case MotionEvent.ACTION_DOWN:
  
              if(pressedUp == false){
                  pressedUp = true;
                  new SendVolumeUpTask().execute();
              }
  
          case MotionEvent.ACTION_UP:
  
              pressedUp = false;
  
      }
          return true;
      }
  });
  </code></pre></li>
  </ol>
  
  <p>I've also tried with simple threads as in <a href="https://stackoverflow.com/questions/7852360/increment-decrement-counter-while-button-is-pressed">Increment-Decrement counter while button is pressed</a> but neither worked. The application works finely for the rest of the buttons (channels, etc.), but volume changing is completely ignored.</p>