简体   繁体   English

当android mobile睡眠时保持wifi连接

[英]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). 我的问题是,移动设备处于“睡眠模式”时,经过一段时间的连接被披露(可能是由于关闭了wifi)。 So as app is unable to send heartbeat, it is also getting disconnected. 因此,由于应用程序无法发送心跳,因此它也断开了连接。

Now how to avoid getting wifi off. 现在如何避免关闭wifi。

I have tried things like 'WIFI_SLEEP_POLICY_NEVER' , WifiManager.WIFI_MODE_FULL etc etc. 我已经尝试过类似'WIFI_SLEEP_POLICY_NEVER'WifiManager.WIFI_MODE_FULL等的功能。

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. 重新编辑:我用过的wifi管理器代码。

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. 我按以下方法使用PowerManager api以及上面的代码,终于解决了。

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(). 对于释放此锁,我要重写onDestroy()。 (Note: Though, it is not recommended. Instead of using onDestroy for releasing, onPause() should be used--as per documentation) (注意:尽管如此,不建议这样做。应该使用onPause()而不是使用onDestroy进行释放-根据文档)

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. 除HTC手机外,它都可以正常工作。

Is there any reason for this? 有什么理由吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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