Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I keep Wifi from disconnecting when phone is asleep?

Tags:

android

I have a service which polls a server at certain intervals. I use an AlarmManager and a BroadcastReceiver to start the service. My problem is that after a certain duration, even though the Wifi is still enabled, but for some reason, my application can't contact the server. I get an "Unreachable network" error.

Note that I've already acquired a partial wake lock as well as a wifilock.

Here's my code for the BroadcastReceiver.

public class ServiceAlarmBroadcastReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        WakeLock wakeLock = null;
        WifiLock wifiLock = null;
        try {
            PowerManager pm = (PowerManager) context
                    .getSystemService(Context.POWER_SERVICE);

            // acquire a WakeLock to keep the CPU running
            wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                    "MyWakeLock");
            if(!wakeLock.isHeld()){
                wakeLock.acquire();
            }

            Log.i("ServiceAlarmBroadcastReceiver", "WakeLock acquired!");


            WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL , "MyWifiLock");
            if(!wifiLock.isHeld()){
                wifiLock.acquire();
            }

            Log.i("ServiceAlarmBroadcastReceiver", "WifiLock acquired!");
            context.startService(new Intent(context, ThePollerService.class));
        } finally {
            // release the WakeLock to allow CPU to sleep
            if (wakeLock != null) {
                if (wakeLock.isHeld()) {
                    wakeLock.release();
                    Log.i("ServiceAlarmBroadcastReceiver", "WakeLock released!");
                }
            }

            // release the WifiLock
            if (wifiLock != null) {
                if (wifiLock.isHeld()) {
                    wifiLock.release();
                    Log.i("ServiceAlarmBroadcastReceiver", "WiFi Lock released!");
                }
            }
        }
    }
}
like image 399
javauser Avatar asked Oct 06 '10 11:10

javauser


People also ask

Why does my Wi-Fi disconnect when in sleep mode?

To save energy, Windows automatically disables the Internet connection when the computer goes into sleep mode. This setting logs off the computer user and severs the computer's connection to the local area network, disrupting office network applications and periodic computer updates and messages.

How do I stay connected to Wi-Fi when my screen is locked?

Swipe down from the status bar, touch and hold the Wi-Fi icon to access the Wi-Fi settings screen, then go to Configure > Keep Wi-Fi on during sleep > Always.

Why does my Wi-Fi turn off when I lock my phone?

Battery-saving Features This can also be common when your phone is in sleep mode. Battery-saving features are one of the most common causes of wifi being shut off on Android.


1 Answers

The problem with the code posted here is that you acquire and release the WakeLock and WifiLock from right inside of your receiver. Unless you are completing your entire task inside of the onStart of your service (which if you are, why even bother having a service???), the locks will be released before your polling task completes.

I would suggest changing your implementation to something like the following:

  1. Have broadcast receiver start service (and that is all)
  2. Have service acquire wake locks and kick off the thread to do your polling operation. The most appropriate spot would be your service onCreate)
  3. After your polled operation is complete, you should stop your polling service
  4. In the onDestroy of your service, you should release the locks you acquired in onStart
like image 96
Justin Breitfeller Avatar answered Nov 10 '22 03:11

Justin Breitfeller