title: Android Service exits leaving imp. data open
body: <p>I am facing a grave problem. Inside a <code>service</code> I am opening Wifi connection and closing it after my task completes. Since, a <code>service</code> exits at any point i face a problem wherein the connection opens and remains open.</p>
  
  <p>Is there a way i can handle this as i am using <code>START_STICKY</code> or i will have to handle it programmatically only? </p>
  
  <p><strong>EDIT</strong> : Can i share my intent information across couple of receivers (<code>BroadcastReceiver</code>). For example, I will write another receiver for action <code>android.net.wifi.wifi_state_changed</code> and my existing receiver is for <code>android.intent.action.PHONE_STATE</code>. 
  IF that can be achieved i can do something about it.</p>
  
  <p><strong>EDIT2</strong> : My code is as follows:</p>
  
  <pre><code>public class CallReceiver extends BroadcastReceiver {
      private static final String LOG_TAG = "CallReceiver";
      private static final String CALL_ACTION = "android.intent.action.PHONE_STATE";
  
      @Override
      public void onReceive(Context context, Intent callIntent)
      {
          Log.d(LOG_TAG, "----------------Inside onReceive of CallReceiver----------------");
  
  
          if (callIntent.getAction().equals(CALL_ACTION))
          {
  
              try
              {
                  Intent myIntent = new Intent(context, MyService.class);
                  context.startService(myIntent);
              }
              catch (Exception e)
              {
                  e.printStackTrace();
                  Log.d(LOG_TAG,"----------------Exception occured while starting service----------------");
              }
          }
      }
  }
  
  
  public class MyService extends Service {
      private Context context;
      private static final String LOG_TAG = "MyService";
      private Thread thread = null;
  
      public MyService() 
      {
          super();
          Log.d(LOG_TAG, "----------------Inside Email Service constructor----------------");
      }
  
      public int onStartCommand(Intent myIntent, int flags, int startId)
      {
          Log.d(LOG_TAG, "----------------Email Service Command Started----------------");
          try
          {
              context = getApplicationContext();
              if(thread == null || !thread.isAlive())
              {
                  thread = new Thread(new MyRunnable("Email Sender", myIntent));
                  thread.start();
              }
          }
          catch (Exception e)
          {
              e.printStackTrace();
              Log.d(LOG_TAG,
                    "----------------Exception occured in Email Service onStartCommand----------------");
          }
          return START_REDELIVER_INTENT;
      }
  
      class MyRunnable implements Runnable {
          String name;
          Intent myIntent;
  
          public MyRunnable(String name, Intent myIntent) {
              this.name = name;
              this.myIntent = myIntent;
          }
  
          @Override
          public void run()
          {
              try
              {
                  doStuff(emailIntent);
              }
              catch (NumberFormatException e)
              {
                  e.printStackTrace();
                  Log.d(LOG_TAG, e.getMessage());
              }
              catch (InterruptedException e)
              {
                  e.printStackTrace();
                  Log.d(LOG_TAG, e.getMessage());
              }
              catch (Exception e)
              {
                  e.printStackTrace();
                  Log.d(LOG_TAG, e.getMessage());
              }
              finally
              {
                  stopSelf();
              }
          }
      }
  
      private void doStuff(Intent emailIntent) throws InterruptedException, Exception
      {
          if (context != null)
          {
              boolean isWifiConnection = false;
  
              try
              {
                  // Check if WiFi connection is available ,if yes try opening it;
                  // Attempt to open WiFi connection
                  isWifiConnection = Utility.isEnableWifiSuccessful(getApplicationContext());
                  Log.d(LOG_TAG, "----------------Wifi conn enabled = " + isWifiConnection
                                 + "----------------");
  
                  if (isWifiConnection)
                  {
                      // Do more stuff
                  }
              }
              catch (Exception e)
              {
                  e.printStackTrace();
                  throw e;
              }
              finally
              {
                  // Code never reaches here !! Somehow, the service stops and by
                  // the time the service stops,
                  // WiFi has been enabled
                  try
                  {
                      if (isWifiConnection)
                      {
                          Utility.isDisableWifiSuccessful(getApplicationContext());
                      }
                  }
                  catch (Exception e)
                  {
                      e.printStackTrace();
                      Log.d(LOG_TAG,
                            "----------------Error occured while closing network connections----------------");
                  }
              }
          }
          else
          {
              Log.d(LOG_TAG, "----------------Context is null----------------");
          }
      }
  
      @Override
      public IBinder onBind(Intent intent)
      {
          // TODO Auto-generated method stub
          return null;
      }
  }
  </code></pre>
  
  <p>Now, if i have another receiver as NetworkReceiver</p>
  
  <pre><code>public class NetworkReceiver extends BroadcastReceiver {
  
      private static final String ACTION = "android.net.wifi.WIFI_STATE_CHANGED";
      private static final String LOG_TAG = "NetworkReceiver";
  
      @Override
      public void onReceive(Context context, Intent networkIntent)
      {
          if(networkIntent.getAction().equals(ACTION))
          {
              Log.d(LOG_TAG, "----------------Inside Network Receiver----------------");
              //Do something which will keep track who has opened the WiFi connection
          }
      }
  }
  </code></pre>
  
  <p>then can <code>myIntent</code> and <code>networkIntent</code> share information and can <code>MySerivce</code> read that information.</p>
  
  <p>Any help would be really grateful.</p>