title: Android: Stop/start service created in onCreate()
body: <p>I currently have a service that is started within the onCreate method of an activity using:</p>
  
  <pre><code>Intent intentService = new Intent(this, MainService.class);
  this.startService(intentService);
  </code></pre>
  
  <p>I need to now be able to stop this service on a button press and restart it again on another button press, however I am unsure how to stop this service and start it again out side of the onCreate method.</p>
  
  <p>I guess I would need to start the service in a different way than what I am currently doing? But I am unsure on the best method for this.</p>
  
  <p>I had looks at <a href="https://stackoverflow.com/questions/5555765/stop-service-in-android">stop service in android</a> but their method of starting the service seems not to work within onCreate.</p>
  
  <p>A more complete over view of my code:</p>
  
  <pre><code>public class MainActivity extends Activity {
  
      protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
  
              lock = (Button) this.findViewById(R.id.lock);
              unlock = (Button) this.findViewById(R.id.unlock);
  
              lock.setOnClickListener(btn_lock);
              unlock.setOnClickListener(btn_unlock);
  
              unlock.setVisibility(View.VISIBLE);
  
              lock.setVisibility(View.GONE);
  
              Intent intentService = new Intent(this, MainService.class);
              this.startService(intentService);
  
          }
  private OnClickListener btn_lock = new OnClickListener() {
          public void onClick(View v) {
                  unlock.setVisibility(View.VISIBLE);
                  lock.setVisibility(View.GONE);
  
  
          }
      };
  private OnClickListener btn_unlock = new OnClickListener() {
          public void onClick(View v) {
                  unlock.setVisibility(View.GONE);
                  lock.setVisibility(View.VISIBLE);
  
          }
      };
  </code></pre>