title: How do I change the rate or period of a repeating task using ScheduledExecutorService?
body: <p>I have a modified version of the bluetooth chat sample app.  I have set up a <code>ScheduledExecutorService</code> which sends a command over bluetooth at a predefined rate using <code>scheduleAtFixedRate</code>.</p>
  
  <p>I have set up a <code>PreferenceActivity</code> to allow the period to be modified by the user.  But I'm unsure how to get the actual recurring tasks to happen with the updated period.  Do I need to cancel and restart the <code>ScheduledExecutorService</code> somehow?</p>
  
  <p>Here's the relevant parts of my code.</p>
  
  <pre><code>private ScheduledExecutorService scheduleTaskExecutor;
  
  public long ReadInterval = 1;
  
  ...    
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
          scheduleTaskExecutor = Executors.newScheduledThreadPool(5);
  ...
      // This schedule a task to run every 1 second:
      scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
        public void run() {
  
  
          // If you need update UI, simply do this:
           runOnUiThread(new Runnable() {
              public void run() {
              // update your UI component here.
                if (connected == true) {
                    sendMessage("READ");                
                    if (D) Log.i(TAG, "In Run!");                   
                }
              }
          });
        }
      }, 0, ReadInterval, TimeUnit.SECONDS);      
      }
  </code></pre>
  
  <p>And I was trying to update <code>ReadInterval</code> here.  The <code>ReadInterval</code> is getting updated but the recurring command period does not get updated.</p>
  
  <pre><code>    @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      if (D)
          Log.d(TAG, "onActivityResult " + resultCode);
      switch (requestCode) {
      case REQUEST_CONNECT_DEVICE:
  ...
      case REQUEST_ENABLE_BT:
  ...
      case REQUEST_SETTINGS:
          // When returning from settings activity
          SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
          String Pref = sharedPref.getString(SettingsActivity.KEY_PREF_READINTERVAL, "");
          ReadInterval = Long.valueOf(Pref);
          Toast.makeText(this, Pref,
                      Toast.LENGTH_SHORT).show();
  
          Log.d(TAG, "Settings Activity Result");
      }
  }
  </code></pre>