简体   繁体   中英

Keeping wifi connection on when android mobile sleeps

I am creating networked app, which sends heartbeat to server after some interval. My problem is when mobile is in 'sleep mode', after some time connection is disclosed(probably due to getting wifi off). So as app is unable to send heartbeat, it is also getting disconnected.

Now how to avoid getting wifi off.

I have tried things like 'WIFI_SLEEP_POLICY_NEVER' , WifiManager.WIFI_MODE_FULL etc etc.

But nothing works? Has anybody has solved this issue? I will be greatfull, if there is any real example.

Thank You in advance

Reedit: wifi manager code which i have used.

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
Lock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL, "LockTag");
lock.acquire();

[Solved]

int pol = android.provider.Settings.System.WIFI_SLEEP_POLICY_NEVER;
                android.provider.Settings.System.putInt(cr, android.provider.Settings.System.WIFI_SLEEP_POLICY, pol);

I used PowerManager api as follows along with above code & finally it's solved.

final PowerManager p = (PowerManager) getSystemService(Context.POWER_SERVICE);
l = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "newTAG");
l.acquire();

& for releasing this lock, I am overriding onDestroy(). (Note: Though, it is not recommended. Instead of using onDestroy for releasing, onPause() should be used--as per documentation)

Recently I tried,

int pol = android.provider.Settings.System.WIFI_SLEEP_POLICY_NEVER;
                android.provider.Settings.System.putInt(cr, android.provider.Settings.System.WIFI_SLEEP_POLICY, pol);

It works fine except for HTC mobiles.

Is there any reason for this?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM