title: Is it wrong to Call an IntentService from a Service?
body: <p>I have a service that is started when the main application starts. I call the Service using this code </p>
  
  <pre><code>Intent serviceIntent = new Intent(this, SMS_Listener_Service.class);
  startService(serviceIntent);
  </code></pre>
  
  <p>The Service itself has the following main notable code </p>
  
  <pre><code>public class SMS_Listener_Service extends Service {
      @Override
          public int onStartCommand(Intent intent, int flags, int startId) {
          super.onStartCommand(intent, flags, startId);
          if (!blnAutoSyncisEnabled) {
              stopSelf();
              return 0;
          } else {
              return START_STICKY;
  
      }
          private class MyContentObserver extends ContentObserver {
  
              public MyContentObserver() {
                  super(null);
              }
  
              @Override
              public void onChange(boolean selfChange) {
                  super.onChange(selfChange);
                  //pause for 3 seconds and see if the counter changes. 
                   if it goes past 5  transactions then start processing and start a timer
                  TaskToRunAfter3Seconds();
                  intTimesCalled = intTimesCalled + 1;
              }
  
          }
  
         }
          private void TaskToRunAfter3Seconds() {
  
          try {
              //calls the ReadTrandactions intentService that parses sms messages
              Intent intent = new Intent(context,   ReadTransactionsForSERVICE.class);
              startService(intent);
  
          } catch (NumberFormatException e) {
  
              BugSenseHandler.sendException(e);
          }
  }
  
  
  }
  </code></pre>
  
  <p>Then we have the called intent Service in which i handle the work in the OnHandleIntent Method</p>
  
  <pre><code>public class ReadTransactionsForSERVICE extends IntentService {
  
      public ReadTransactionsForSERVICE() {
          super("ReadTransactionsForSERVICE");
       }
     protected void onHandleIntent(Intent intent) {
          //Initialize stuff
         try {
          //do the main work here.
         } catch (Exception e) {
  
          BugSenseHandler.sendException(e);
           return;
         }
  
      }
  
  
  }
  </code></pre>
  
  <p>My question is, Is my implementation sound? Ie <strong>is it ok to call an IntentService from a BroadcastReceiver in a Service?</strong></p>
  
  <p>I ask this because , My Service hangs in the most odd fashion and I suspect the IntentService called from Service could be the issue. (I asked a similar question earlier but got no response. this one is more detailed. <a href="https://i.stack.imgur.com/SAXUY.jpg" rel="nofollow noreferrer">1</a>: <a href="https://stackoverflow.com/posts/21057789/edit">https://stackoverflow.com/posts/21057789/edit</a>)</p>
  
  <p>For some reason it Hangs and cannot be stopped even by memory or task managers. Physically tapping "Stop" in the service itself does nothing. </p>
  
  <p><img src="https://i.stack.imgur.com/SAXUY.jpg" alt="enter image description here"></p>
  
  <p>Uninstalling the app doesn't work either because when you go the running processes you see an item "Unknown process.....restarting" and if you try kill that then the applications manager generates an error ie "Settings has stopped" but on return the Unknown process is still there.</p>
  
  <p>If you then Install the app again while the above is showing, the service never starts. it again remains in the "restarting" mode.
  The only cure is to Restart the Phone itself. (cold boot)</p>
  
  <p>The above has been observed only on android 4.3 and above. Lower versions don't seem to have this issue.</p>
  
  <p>Thanks and looking forward to some help.</p>
  
  <p>I call the service from 3 points depending on the scenario. first is on application startup Where I check if the Service is up and if not i start it.</p>
  
  <p>Second is from the BroadcastReceiver </p>
  
  <pre><code>public class SMSNotifyStarter extends BroadcastReceiver {
  
      @Override
      public void onReceive(Context context, Intent intent) {
          Intent service = new Intent(context, SMS_Listener_Service.class);
          context.startService(service);
          //(this,"Boot Complete Recieved and Service Started.");
      }
  }
  </code></pre>
  
  <p>and finally from a network State change. ie if a new internet connection is made active (Wifi or Mobile) I check for new Transactions. code here</p>
  
  <pre><code>public class NetworkChangeReceiver extends BroadcastReceiver {
      Context ctx;
      @Override
      public void onReceive(final Context context, final Intent intent) {
          ctx = context;
          int status = NetworkUtil.getConnectivityStatusString(context);
          if (status == 1 || status == 2) { //means wifi or mobile is active
          blnSyncAllowed = GSR.ReadSharedPreferencesBool(ctx, 
                               GSR.PrefsFileName,"SYNC_ALLOWED", true);
  
          if (blnSyncAllowed)) {
              Intent intentx = new Intent(context, ReadTransactionsForSERVICE.class);
              intentx.putExtra("CalledByWakeOnConnectionFound", "WakeOnConnectionFound");
              context.startService(intentx);
  
            }
          }
  
         //Toast.makeText(context, status, Toast.LENGTH_LONG).show();
      }
  
  
  }
  </code></pre>
  
  <p>Manifest entries</p>
  
  <pre><code>    &lt;receiver
          android:name="com.myappName.NetworkChangeReceiver"
          android:label="NetworkChangeReceiver"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.net.conn.CONNECTIVITY_CHANGE" /&gt;
              &lt;action android:name="android.net.wifi.WIFI_STATE_CHANGED" /&gt;
          &lt;/intent-filter&gt;
      &lt;/receiver&gt;
  
      &lt;receiver android:name="com.myappName.SMSNotifyStarter"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.QUICKBOOT_POWERON" /&gt;
              &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt;
          &lt;/intent-filter&gt;
      &lt;/receiver&gt;
  
  
      &lt;service
          android:name="com.myappName.SMS_Listener_Service"
          android:enabled="true"
          android:icon="@drawable/myAppIcon"
          android:label="myAppManager"&gt;
  
          &lt;intent-filter&gt;
              &lt;action android:name="com.myappName.SMS_Listener_Service" /&gt;
          &lt;/intent-filter&gt;
      &lt;/service&gt;
      &lt;service android:name="myappName.ReadTransactionsForSERVICE"&gt;
     &lt;/service&gt;
  </code></pre>