startscan()已导致10分钟后,当手机进入空闲状态 [英] startscan() has result after 10 min when phone get into idle state
问题描述
我试图扫描WiFi网络,每2分钟。我使用的服务的。当手机处于正在使用主动或之后 startscan()
叫我得到 SCAN_RESULTS_AVAILABLE_ACTION
6秒。因此,我可以为wifis定期扫描。但之后手机一直没有感动任何人在一定时间内(10分钟)startscan()停止工作和10分钟得到结果之后。任何人都经历过这样?
I am trying to scan wifi networks every 2 minutes. I am using service for that. When the phone is "active" or "in use" after startscan()
called I get SCAN_RESULTS_AVAILABLE_ACTION
in 6 sec. So i can scan for wifis periodically. But after the phone has not been touched by anyone for a certain time (10 min) startscan() stops "working" and only after 10 min getting result. Anybody experienced this?
推荐答案
根据这我已经找到了解决办法 - 这是因为<一个href="http://developer.android.com/reference/android/provider/Settings.Global.html#WIFI_SLEEP_POLICY_DEFAULT"相对=nofollow>在WiFi休眠策略。 你可以设置你的无线设备永远不会进入睡眠状态,这一点:
According to this I have found the solution - this is because the Wifi sleep policy. You can set your wifi device never goes to sleep with this:
Settings.System.putInt(getContentResolver(),
Settings.System.WIFI_SLEEP_POLICY,
Settings.System.WIFI_SLEEP_POLICY_NEVER);
当然,您在的AndroidManifest.xml
添加此权限的文件:
Be sure, that you added this permission in AndroidManifest.xml
file:
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
在扫描期间将5-15s - 这是自动的 - 你不需要调用 startscan()
。关于Android扫描过程的详细信息,可以发现<一href="http://android.stackexchange.com/questions/37621/where-can-i-find-settings-for-wifi-internal-scan-period">here.
修改
这也许是更好的解决方案,如果你只是想扫描热点:
WIFI_MODE_SCAN_ONLY
< /一> - 它可以通过激活:
the scanning period will be 5-15s - and this is automatic - you dont need to call startscan()
. more info about Android scanning process can be found here.
maybe this is even better solution if you only want to scan for hotspots:
WIFI_MODE_SCAN_ONLY
- it can be activated by:
WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiLock wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_SCAN_ONLY , "MyWifiLock");
if(!wifiLock.isHeld()){
wifiLock.acquire();
}
不要忘记将其释放,约 WifiLock
详细信息的此处
也可以定义此权限:
dont forget to release it, more info about WifiLock
here
also define this permission:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
EDIT2 这个工程,以及:
您可以启用,并定期禁用无线:
Edit2 this works as well:
you can enable, and disable wifi periodically:
WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if(wm.isWifiEnabled() == false) {
wm.setWifiEnabled(true);
}
然后扫描后您的BroadcastReceiver获得包含动作 WIFI_STATE_ENABLED
额外
这篇关于startscan()已导致10分钟后,当手机进入空闲状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!