title: Android dropping tcp connection when screen is off
body: <p>My Android app starts and keeps a TCP connection going within a Foreground service. The activity uses <code>StartService()</code> to start the service. Also the service is started in it's own process.</p>
  
  <p>Here is the services <code>OnStartCommand</code>:</p>
  
  <pre><code>// Code is in C# using Xamarin but regular Java/Android answers are acceptable
  public override StartCommandResult OnStartCommand (Intent intent, StartCommandFlags flags, int startId)
  {
      base.OnStartCommand (intent, flags, startId);
  
      ...
  
      var ongoingNotification = new Notification (Resource.Drawable.icon, "Service running");
      var pendingIntent = PendingIntent.GetActivity (this, 0, new Intent (this, typeof(MainActivity)), 0);
      ongoingNotification.SetLatestEventInfo (this, "Service", "The service is running.", pendingIntent);
      StartForeground ((int)NotificationFlags.ForegroundService, ongoingNotification);
      return StartCommandResult.RedeliverIntent;
  }
  </code></pre>
  
  <p>The connection is fine when the phone's screen is on, whether or not an activity from my app is open or not. However less than 30 seconds after I turn the screen off I always lose tcp connection. My app reconnects automatically so I get the connection back, but it will continually disconnect when the screen is off. I turn it back on and it's fine, even if an activity from my app isn't open.</p>
  
  <p>It could be connected to the Android Lifecycle but I just don't see how. Based on debug messages I write to a text file on the phone (the issue doesn't happen when a debugger from an IDE is attached), the service seems to be operating as it's supposed to but the connection just isn't being stable.</p>
  
  <p>I've also tested this with and without the "Don't Keep Activities" in Developer options selected to no change. So it shouldn't have anything to do with the Activity Lifecycle at least.</p>
  
  <p>Why is Android dropping my tcp connection, but only when the screen is off?</p>