title: Wakelock and wifilock not working
body: <p>I have read plenty of tutorial and posts here on SO regarding the use of WakeLock and WifiLock, but still didn't get to a solution of my issue.</p>
  
  <p>I'm writing an app that has, when you start it, the only effect of creating and starting a (foreground) service. This service run two threads that are an UDP broadcast listener (using java.io) and a TCP server (using java.nio). In the onCreate of the service I acquire a wakelock and a wifilock, and I release them in the onDestroy.</p>
  
  <p>Everything works fine as far as the phone is awake, but when the display goes off the UDP broadcast receiver stops receiving broadcast messages, and will not receive any until I switch on again the display. Practically, the locks are not working at all and there is no difference in put them...where am I wrong? I'm sure I'm doing something stupid somewhere, but can't find it by myself.</p>
  
  <p>Here is some code:</p>
  
  <p>This is what Activity does:</p>
  
  <pre><code>protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      onlyStartService = true;
  }@Override
  protected void onStart() {
      super.onStart();
      Intent bIntent = new Intent(this, FatLinkService.class);
      getApplicationContext().startService(bIntent);
      getApplicationContext().bindService(bIntent, flConnection, BIND_AUTO_CREATE);
  } 
  // service connection to bind idle service
  private ServiceConnection flConnection = new ServiceConnection() {
      public void onServiceConnected(ComponentName className, IBinder binder) {
         linkService.MyLinkBinder flBinder = (LinkService.MylinkBinder) binder;
         flServiceInstance = flBinder.getService();
         if (onlyStartService) {
             condLog("Service bound and finishing activity...");
             finish();
         }
      }
      public void onServiceDisconnected(ComponentName className) {
          flServiceInstance = null;
      }
  }; 
  @Override
  protected void onStop() {
      super.onStop();
  
      if (fatFinish) {
          Intent bIntent = new Intent(this, FatLinkService.class);
          flServiceInstance.stopServices();
          flServiceInstance.stopForeground(true);
          flServiceInstance.stopService(bIntent);
          condLog("Service stop and unbound");
          flServiceInstance = null;
      }
      getApplicationContext().unbindService(flConnection);
  }
  </code></pre>
  
  <p>This is how service is:</p>
  
  <pre><code>public class LinkService extends Service {
      InetAddress iaIpAddr, iaNetMask, iaBroadcast;
      private final IBinder mBinder = new MyLinkBinder();
      private linklistenBroadcast flBroadServer = null;
      private linkTCPServer flTCPServer = null;
      private linkUDPClient flBroadClient = null;
      List&lt;String&gt; tokens = new ArrayList&lt;String&gt;();
      private PowerManager.WakeLock wakeLock;
      private WifiManager.WifiLock wifiLock;
  
  public class MylLinkBinder extends Binder {
      lLinkService getService() { return LinkService.this; }
  }
  
  @Override
  public IBinder onBind(Intent intent) {
      return mBinder;
  }
  
  @Override
  public void onCreate() {
      super.onCreate();
      getLocks();
  }
  
  @Override
  public int onStartCommand(Intent intent, int flags, int startId)
  {        
      instantiateServices();
      // notifies presence to other fat devices
      condLog("Service notifying fat presence...");
      flBroadClient = new LinkUDPClient();
      flBroadClient.startSending(LinkProtocolConstants.BRCMD_PRESENCE + String.valueOf(LinkProtocolConstants.tcpPort), iaBroadcast, LinkProtocolConstants.brPort);
      return START_STICKY;
  }
  
  public void getLocks() {
      // acquire a WakeLock to keep the CPU running
      condLog("Acquiring power lock");
      WifiManager wm = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
      wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL , "MyWifiLock");
      wifiLock.acquire();
      PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
      wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
      wakeLock.acquire();
  }
  
  public void stopServices() {
      if (flTCPServer != null)
          flTCPServer.stopServer();
      if (flBroadServer != null)
          flBroadServer.stopSelf();
  }
  
  private void instantiateServices() {
      populateAddresses(); // just obtain iaIpAddr  
      if (flTCPServer == null) {
          condLog("Instantiating TCP server");
          flTCPServer = new LinkTCPServer(iaIpAddr, FatLinkProtocolConstants.tcpPort);
          flTCPServer.execute();
      }
      if (flBroadServer == null) {
          condLog("Instantiating UDP broadcast server");
          Intent notifyIntent = new Intent(this, LinkMain.class); // this is the main Activity class
          notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
          notifyIntent.setAction("FROM_NOTIFICATION");
          PendingIntent notifyPIntent = PendingIntent.getActivity(this, 0, notifyIntent, 0);
  
          Notification fixNotification = new Notification.Builder(getApplicationContext())
                  .setContentTitle("Link")
                  .setSmallIcon(R.mipmap.imgLink)
                  .setContentIntent(notifyPIntent)
                  .build();
          startForeground(1234, fixNotification);
          flBroadServer = new LinklistenBroadcast();
          flBroadServer.start();
      }
  }
  
  private final class LinklistenBroadcast extends Thread {
      private boolean bStopSelf = false;
      DatagramSocket socket;
      public void stopSelf() {
          bStopSelf = true;
          socket.close();
      }
  
      @Override
      public void run() {
          condLog( "Listening broadcast thread started");
          bStopSelf = false;
          try {
          //Keep a socket open to listen to all the UDP trafic that is destinated for this port
          socket = new DatagramSocket(null);
          socket.setReuseAddress(true);
          socket.setSoTimeout(LinkGeneric.BR_SOTIMEOUT_MILS);
          socket.setBroadcast(true);
          socket.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), FatLinkProtocolConstants.brPort));
          while (true) {
                  condLog("Ready to receive broadcast packets...");
                  //Receive a packet
                  byte[] recvBuf = new byte[1500];
  
                  DatagramPacket packet = new DatagramPacket(recvBuf, recvBuf.length);
                  try {
                      socket.receive(packet);
                  } catch (InterruptedIOException sException) {
                      condLog(sockExcept.toString());
                      break;
                  } catch (SocketException sockExcept) {
                     condLog(sockExcept.toString());
                  }
                  if (bStopSelf) {
                      condLog("Broadcast server stopped...");
                      break;
                  }
                  int len = packet.getLength();
                  String datarecvd = new String(packet.getData()).trim();
                  //datarecvd = datarecvd.substring(0, len);
                  //Packet received
                  String message = new String(packet.getData()).trim();
                  condLog("&lt;&lt;&lt; broadcast packet received from: " + packet.getAddress().getHostAddress() + " on port: " + packet.getPort() + ", message: " + message);
                  if (packet.getAddress().equals(iaIpAddr)) {
                      condLog("Ooops, it's me! discarding packet...");
                      continue;
                  }
                  else
                      condLog("&lt;&lt;&lt; Packet received; data size: " + len + " bytes, data: " +  datarecvd);
  
                  //See if the packet holds the right command (message)
  
                  // protocol decode
                  // here do some tuff
          } catch (IOException ex) {
              condLog(ex.toString());
          }
  
          if (socket.isBound()) {
              condLog( "Closing socket");
              socket.close();
          }
          condLog( "UDP server thread end.");
          flTCPServer = null;
          flBroadServer = null;
      }
  
      public boolean isThreadRunning() {
          return !bStopSelf;
      };
  }
  
  // Utility functions
  public boolean checkBroadcastConnection (DatagramSocket socket, int timeOutcycles) {
      int tries = 0;
      while (!socket.isConnected()) {
          tries++;
          if (tries &gt;= timeOutcycles)
              return false;
      }
      return true;
  }
  
  @Override
  public void onDestroy() {
      super.onDestroy();
      if (wakeLock != null) {
          if (wakeLock.isHeld()) {
              wakeLock.release();                
          }
      }
      if (wifiLock != null) {
          if (wifiLock.isHeld()) {
              wifiLock.release();
          }
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And, finally, here is the manifest:</p>
  
  <pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="xxxxxx.ink" &gt;
      &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /&gt;
      &lt;uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /&gt;
      &lt;uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /&gt;
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
      &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;
      &lt;uses-permission android:name="android.permission.WRITE_SETTINGS" /&gt;
      &lt;application
          android:allowBackup="true"
          android:icon="@mipmap/imgLinkmascotte"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name=".LinkMain"
              android:label="@string/app_name"
              android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;service android:name=".LinkService" /&gt;
      &lt;/application&gt;
  &lt;/manifest&gt;
  </code></pre>
  
  <ul>
  <li><p>I have read <a href="https://stackoverflow.com/questions/14911700/wifilock-and-wakelock-not-working-correctly-on-android">this</a>, and I have the doubt the problem is the same but it is actually happening to me on all the phone I have tried (galaxy Tab 7.1, galaxy tag 10, Galaxy SIII, Galaxy Note 3 Neo, Galaxy SIII mini) with android releases from 4.0 to 4.4.</p></li>
  <li><p>I have already tried the solution posted <a href="http://PowerManager.ACQUIRE_CAUSES_WAKEUP" rel="nofollow noreferrer">here</a>, but anything changed (again...it is a bit frustrating).</p></li>
  <li><p>I have even tried to set the "Keep wifi option in standby" to "Always" in all the phones I've tried, but still nothing.</p></li>
  <li><p>I have studied the WakeFulIntentService class, that is supposed to work as everybody is saying that it is, but I can't see any significant different in my code.</p></li>
  </ul>
  
  <p>I really hope someone can help me, I'm really stuck on this since last week.</p>
  
  <p>EDIT: following the waqaslam answer, I have checked on a device that has "Wifi optimisation" option in Wifi-Advaced settings, and it actually works as soon as I uncked the option. So, now, the problem become: can I disable Wifi optimisation in devices that haven't that option shown in the advanced menu? as I wrote below in my comment, this seems not to be related to android release, as I have two devices (both Samsung) with 4.4.2 and they are not showing the option.</p>
  
  <p>New EDIT: from the edited waqaslam answer, I have tried to add multicastlock to my service, but again anything changed. This is getting annoying, there's hardly something easy and clear to do with android. </p>
  
  <p>thank you very much
  C.</p>