title: Cannot Remove View From WindowManager - Multiple Threads
body: <p>I am having an issue with removing a view from a WindowManager. Here is the method I am calling to do it:</p>
  
  <pre><code>public void updateDockNow (String jsonData) {  
      getWM().removeView(getView());
      setView(new Stuff(jsonData));
      getWM().addView(getView(), getParams());
  }
  </code></pre>
  
  <p>It works perfectly when called from this onChange method initialized in the onCreate.</p>
  
  <pre><code>observer = new ContentObserver(new Handler()){
      @Override
  public void onChange(boolean selfChange) {
      super.onChange(selfChange);
      updateDockNow(DockPicker.getDockStringAsync(DockService.this));
  }};
  </code></pre>
  
  <p>However, it fails when called from this, also initialized in onCreate.</p>
  
  <pre><code>final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);       
  scheduler.scheduleAtFixedRate(new Runnable() {
      public void run() { 
          if (decision) { updateDockNow(info); } // Problem
      }
  }, 0, 1, TimeUnit.SECONDS);
  </code></pre>
  
  <p>Can I not call the updateDockNow from another thread? Why does it work in one case but not the other?</p>
  
  <p>NOTE: This is not being run from an Activity, it is from a Service.</p>
  
  <p>Thank you</p>
  
  <p>-- ANSWER --</p>
  
  <pre><code>final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);       
  scheduler.scheduleAtFixedRate(new Runnable() {
      public void run() { 
          if (decision) { threadMsg(item); } // Problem
      }
  
      private void threadMsg(String msg) {
          if (!msg.equals(null) &amp;&amp; !msg.equals("")) {
              Message msgObj = handler.obtainMessage();
              Bundle b = new Bundle();
              b.putString("message", msg);
              msgObj.setData(b);
              handler.sendMessage(msgObj);
          }
      }
  
      private final Handler handler = new Handler() {
          public void handleMessage(Message msg) {
              String reply = msg.getData().getString("message");
  
              if ((null != reply)) {
                  Log.i(TAG, "SETTINGS UPDATED, hurray!");
                  getWM().removeView(getView());
                  setView(new Dock(DockService.this, reply));
                  getWM().addView(getView(), getParams());
              }}
      };
  }, 0, 1, TimeUnit.SECONDS);
  </code></pre>