title: Stop IntentService from inside my application?
body: <p>I'm building some kind of chat program that uses GCM to notify the user that a message is available. I followed the gcm client example using a <code>WakefulBroadcastReceiver</code> and <code>IntentService</code> and everything works as expected.</p>
  
  <p>The <code>BroadcastReceiver</code>:</p>
  
  <pre><code>public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
  
      @Override
      public void onReceive(Context context, Intent intent) {
          // Explicitly specify that GcmIntentService will handle the intent.
          ComponentName comp = new ComponentName(context.getPackageName(), GcmIntentService.class.getName());
          // Start the service, keeping the device awake while it is launching.
          startWakefulService(context, (intent.setComponent(comp)));
          setResultCode(Activity.RESULT_OK);
      }
  }
  </code></pre>
  
  <p>and the <code>IntentService</code>:</p>
  
  <pre><code>public class GcmIntentService extends IntentService {
      public static final int NOTIFICATION_ID = 1;
      private NotificationManager mNotificationManager;
  
      public GcmIntentService() {
          super("GcmIntentService");
      }
  
      public static final String TAG = "IOAN";
  
      @Override
      protected void onHandleIntent(Intent intent) {
          Bundle extras = intent.getExtras();
          GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
          // The getMessageType() intent parameter must be the intent you received
          // in your BroadcastReceiver.
          String messageType = gcm.getMessageType(intent);
  
          if (!extras.isEmpty()) {  // has effect of unparcelling Bundle
              if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                  // Post notification of received message.
                  sendNotification(extras.getString("sender"), extras.getString("message"));
                  Log.i(TAG, "Received: " + extras.toString());
              }
          }
          // Release the wake lock provided by the WakefulBroadcastReceiver.
          GcmBroadcastReceiver.completeWakefulIntent(intent);
      }
     //... more stuff
  }
  </code></pre>
  
  <p>So the <code>IntentService</code> sends a notification to my application. </p>
  
  <p>Now, when the user closes my application (back button -> are you sure you want to exit? -> yes), I want to also have a check box "do you still want to receive messages?" (or something like that), and if the user chooses not to receive messages, I want to stop the service that handles the gcm message and restart it when the app runs again. </p>
  
  <p>How do I do that?</p>
  
  <p>Edit: I tried <code>stopService(new Intent(MainActivity.this,GcmIntentService.class));</code> but I still get the notifications.</p>
  
  <p>Edit 2: Seems like a <code>boolean</code> value in <code>SharedPreferences</code> works fine... any better solution?</p>