title: Service with Timer calling another Service
body: <p>I am managing data from <strong>Server to Client</strong> and <strong>Client to Server</strong> using <code>Service</code>. In that I am calling one <code>Service</code> after Login:</p>
  
  <pre><code>context.startService(new Intent(LoginActivity.this, CheckAutoSyncReceivingOrder.class));
  context.startService(new Intent(LoginActivity.this, CheckAutoSyncSendingOrder.class));
  </code></pre>
  
  <p>I have called one timer in above both <code>Service</code> </p>
  
  <h3>CheckAutoSyncReceivingOrder Service:</h3>
  
  <blockquote>
    <p>Its calling another service named <code>ReceivingOrderService</code> on Every 1
    minute to get updated data from server.</p>
  </blockquote>
  
  <pre><code>public class CheckAutoSyncReceivingOrder extends Service {
  
      Timer timer;
  
      @Override
      public IBinder onBind(Intent arg0) {
          // TODO Auto-generated method stub
          Log.i(TAG, "CheckAutoSyncReceivingOrder Binding Service...");
          return null;
      }
  
      @Override
      public void onStart(Intent intent, int startId) {
          // TODO Auto-generated method stub
  
          if (timer != null) {
              timer.cancel();
              Log.i(TAG, "RECEIVING OLD TIMER CANCELLED&gt;&gt;&gt;");
          }
  
          timer = new Timer();
          timer.schedule(new TimerTask() {
              @Override
              public void run() {
                  Log.i(TAG, "&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; RECEIVING AUTO SYNC SERVICE &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;");
                  if (InternetConnection.checkConnection(getApplicationContext())) {
                      if (getDatabasePath(DatabaseHelper.DATABASE_NAME).exists())
                          startService(new Intent(
                                  CheckAutoSyncReceivingOrder.this,
                                  ReceivingOrderService.class));
                  } else {
                      Log.d(TAG, "Connection not available");
                  }
              }
          }, 0, 60000); // 1000*60 = 60000 = 1 minutes
      }
  
      @Override
      public void onDestroy() {
          // TODO Auto-generated method stub
  
          if (timer != null)
              timer.cancel();
  
          Log.d(TAG, "Stopping Receiving...");
  
          super.onDestroy();
      }
  }
  </code></pre>
  
  <h3>CheckAutoSyncSendingOrder Service:</h3>
  
  <blockquote>
    <p>Its calling another service named <code>SendingOrderService</code> on Every 2.5
    minute to send updated data to server.</p>
  </blockquote>
  
  <pre><code>public class CheckAutoSyncSendingOrder extends Service {
  
      Timer timer;
  
      @Override
      public void onStart(Intent intent, int startId) {
          // TODO Auto-generated method stub
  
          if (timer != null) {
              timer.cancel();
              Log.i(TAG, "OLD TIMER CANCELLED&gt;&gt;&gt;");
          }
  
          timer = new Timer();
  
          timer.schedule(new TimerTask() {
              @Override
              public void run() {
                  Log.i(TAG, "&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; SENDING AUTO SYNC SERVICE &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;");
                  if (InternetConnection.checkConnection(getApplicationContext())) {
                      if (getDatabasePath(DatabaseHelper.DATABASE_NAME).exists())
                          startService(new Intent(CheckAutoSyncSendingOrder.this,
                                  SendingOrderService.class));
                  } else {
                      Log.d(TAG, "connection not available");
                  }
              }
          }, 0, 150000); // 1000*60 = 60000 = 1 minutes * 2.5 = 2.5 =&gt;Minutes
      }
  
      @Override
      public void onDestroy() {
          // TODO Auto-generated method stub
  
          if (timer != null)
              timer.cancel();
  
          Log.d(TAG, "Stopping Sending...");
  
          super.onDestroy();
      }
  
      @Override
      public IBinder onBind(Intent intent) {
          // TODO Auto-generated method stub
          return null;
      }
  </code></pre>
  
  <p>Both Activity will running till Internet off. It will automatically call again when Internet Connection Available.</p>
  
  <p>Main thing is I am getting problem <strong>While destroying activity services calls automatically</strong>. </p>
  
  <p>Is there any solution or way to change flow for same thing?</p>
  
  <p>Advance Thanking you.</p>