How can I turn off Wi-Fi when the phone is sleeping?

I have a service that checks the server at regular intervals. I use AlarmManager and BroadcastReceiver to start the service. My problem is that after a certain duration, although Wi-Fi is still turned on, but for some reason my application cannot contact the server. I get an "Unreachable network" error message.

Please note that I have already acquired a partial tracking lock as well as wifilock.

Here is my code for 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!"); } } } } } 
+7
android
source share
4 answers

The problem with the code here is that you purchase and release WakeLock and WifiLock right inside your receiver. If you have not completed your entire task inside the onStart of your service (what if you, why even worry about providing the service?), Locks will be released before the completion of your survey task.

I would suggest changing your implementation to something like this:

  • You have a broadcast launch service (and that’s all)
  • Acquire service locks and run the thread to perform a polling operation. The most appropriate place would be your onCreate service).
  • After completing the polling operation, you must stop your polling
  • In onDestroy of your service, you must release the locks that you acquired on onStart
+6
source share

Thanks to Tom, I was able to solve this problem. Here is the code:

 Settings.System.putInt(getContentResolver(), Settings.System.WIFI_SLEEP_POLICY, Settings.System.WIFI_SLEEP_POLICY_NEVER); 
+3
source share

In the section "WiFi Settings", "Menu Key", "Advanced Settings" there is a list of options WIFI_SLEEP_POLICY, which, when installed, will never support a Wi-Fi connection during sleep mode.

You can manipulate this in the Settings Panel. System package.

http://developer.android.com/reference/android/provider/Settings.System.html#WIFI_SLEEP_POLICY

Hope this helps,

Tom

0
source share

Edit javauser answer a bit:

 private void setNeverSleepPolicy() { try { ContentResolver cr = getContentResolver(); if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN) { int set = android.provider.Settings.System.WIFI_SLEEP_POLICY_NEVER; android.provider.Settings.System.putInt(cr, android.provider.Settings.System.WIFI_SLEEP_POLICY, set); } else { int set = android.provider.Settings.Global.WIFI_SLEEP_POLICY_NEVER; android.provider.Settings.System.putInt(cr, android.provider.Settings.Global.WIFI_SLEEP_POLICY, set); } } catch (Exception e) { e.printStackTrace(); } } 
0
source share

All Articles