title: Stopping a Android service in an Activity
body: <p>I'm having trouble STOPPING the StimulationService , I'm not sure if i'm calling the stopservice method correctly from my activity. 
  Any help will be much appreciated.</p>
  
  <p><strong>Activity to start and stop Service</strong></p>
  
  <pre><code>     public class Stimulation extends Activity implements OnClickListener {
    private static final String TAG = "StimulationActivity";
    Button buttonStart, buttonStop;
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(com.someapp.Activities.R.layout.stimulation);
  
      buttonStart = (Button) findViewById(com.someapp.Activities.R.id.ButtonStart);
      buttonStop = (Button) findViewById(com.someapp.Activities.R.id.ButtonStop);
  
      buttonStart.setOnClickListener(this);
      buttonStop.setOnClickListener(this);
    }
  
    public void onClick(View src) {
      switch (src.getId()) {
      case com.someapp.Activities.R.id.ButtonStart:
        Log.d(TAG, "onClick: starting service");
        startService(new Intent(this, StimulationService.class));
        break;
      case com.someapp.Activities.R.id.ButtonStop:
        Log.d(TAG, "onClick: stopping service");
        stopService(new Intent(this, StimulationService.class));
        break;
      }
    }
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>Service</strong></p>
  
  <pre><code>     public class StimulationService extends Service {
  private static final String TAG = "StimulationService";
  private IOIO ioio_;
  private DigitalOutput led       
  
  
  private volatile IOIOThread ioio_thread_;
  
  public IBinder onBind(Intent intent) {
      return null;
  }
  
  
  public void onCreate() {
      Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();       
      Log.d(TAG, "onCreate");
  
  }
  
  public void onDestroy() {
      Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
      Log.d(TAG, "onDestroy");
      ioio_thread_.stop();
  
  }
  
  public void onStart(Intent intent, int startid) {
      Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
      Log.d(TAG, "onStart");
      ioio_thread_ = new IOIOThread();
      ioio_thread_.start();
  
  }
  
  public void onStop(Intent intent, int stopid) {
      Log.d(TAG, "stop()");
      ioio_thread_ = null;
  }
  
  
  class IOIOThread extends Thread {
      private IOIO ioio_;
      private DigitalOutput led;
  
      /** Thread body. */
      public void run() {
          Thread thisThread = Thread.currentThread();
          super.run();
  
          while (ioio_thread_ == thisThread) {
              ioio_ = IOIOFactory.create();
              try{
                  Log.d(TAG, "Wait for IOIO Connection");
                  ioio_.waitForConnect();
                  Log.d(TAG, "IOIOConnected");
  
                  while (true) {
                      intializePins();
                      Log.d(TAG, "Pins Intialized");
                      while(true){
                          led.write(false);
                          sleep(2000);
                          led.write(true);
                          sleep(2000);
                      }
                  }
  
              }
  
  
              catch (ConnectionLostException e) {
              } catch (Exception e) {
                  Log.e("Hello", "Unexpected exception caught", e);
                  ioio_.disconnect();
                  break;
              } finally {
                  try {
                      ioio_.waitForDisconnect();
                  } catch (InterruptedException e) {
                  }
              }
          }
      }
  </code></pre>
  
  <p>}</p>