title: Keep wifi active in foreground service after phone goes to sleep
body: <p>I want to receive packets from wifi when my phone is locked. The problem is that when I lock my screen, my foreground service stops receiving packets. I'm using Foreground Service like this:</p>
  
  <pre><code>public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
  {
      var notification = new Notification.Builder(this)
          .SetContentTitle(Resources.GetString(Resource.String.app_name))
          .SetContentText(Resources.GetString(Resource.String.notification_text))
          .SetSmallIcon(Resource.Drawable.ic_stat_name)
          .SetContentIntent(BuildIntentToShowMainActivity())
          .SetOngoing(true)
          .AddAction(BuildRestartTimerAction())
          .AddAction(BuildStopServiceAction())
          .Build();
  
  
      // Enlist this instance of the service as a foreground service
      StartForeground(Constants.SERVICE_RUNNING_NOTIFICATION_ID, notification);
  
      /*DO THIS EVEN WHEN SCREEN IS LOCKED*/
  
      var powerManager = (PowerManager)GetSystemService(PowerService);
      _wakeLock = powerManager.NewWakeLock(WakeLockFlags.Partial, "WakeLockTag");
      _wakeLock.Acquire();
  
      var wifiManager = (WifiManager)GetSystemService(WifiService);
      _wifiLock = wifiManager.CreateWifiLock(WifiMode.FullHighPerf, "xamarin_wifi_lock");
      _wifiLock.Acquire();
  
      if (!powerManager.IsIgnoringBatteryOptimizations("com.xamarin.xample.foregroundservicedemo") ||
          !_wakeLock.IsHeld || !_wifiLock.IsHeld)
          throw new InvalidOperationException("OPTIMIZATIONS NOT ACTIVE");
  
      string msg = timestamper.GetFormattedTimestamp();
      Log.Debug(TAG, msg);
      Intent intent = new Intent(Constants.NOTIFICATION_BROADCAST_ACTION);
      intent.SetAction(Android.Provider.Settings.ActionIgnoreBatteryOptimizationSettings);
      intent.PutExtra(Constants.BROADCAST_MESSAGE_KEY, msg);
      LocalBroadcastManager.GetInstance(this).SendBroadcast(intent);
      Task.Run(() =&gt;
      {
          using (var client = new UdpClient(12345))
          {
              while (true)
              {
                  var result = client.ReceiveAsync().Result;
                  Console.WriteLine($"RECEIVED: {result.Buffer.Length}");
              }
          }
      });
  
      return StartCommandResult.Sticky;
  }
  </code></pre>
  
  <p>I'm doing the following things to make sure it is <strong>not killed</strong>:</p>
  
  <ol>
  <li>Starting Foreground Service</li>
  <li>Using <code>StartCommandResult.Sticky</code></li>
  <li>Using Wake Lock</li>
  <li>Using Wifi Lock</li>
  <li>Setting WifiSleepPolicy to <code>Never</code> (I have it setup in my phone
  settings)</li>
  <li>Setting <code>ActionIgnoreBatteryOptimizationSettings</code> in intent</li>
  <li>Whitelisting my app through adb command prompt while debugging</li>
  </ol>
  
  <p>What else am I missing?    I am using Samsung A5 with Android 6.0 - API 23.</p>
  
  <p>I looked into logs from adb command prompt and I checked that my service is in fact running as Foreground Service and all locks are held.</p>