我写过一些使用Wifi作为可能的互联网连接媒体的软件.. 我确实注意到,经过一段时间(我在15-20分钟左右猜测),设备进入省电模式后(无论如何,当屏幕变黑时)Wifi连接将被简单地丢弃,即使它是仍在使用:(这在ADP 2上(因此是Android 1.6的Google Ion)... 我能够得到以下日志:
06-10 15:04:27.009: DEBUG/WifiService(72): got ACTION_DEVICE_IDLE
06-10 15:04:27.069: ERROR/wpa_supplicant(538): Set_key: Wrong Key
06-10 15:04:27.069: ERROR/wpa_supplicant(538): Set_key: Wrong Key
06-10 15:04:27.069: ERROR/wpa_supplicant(538): Set_key: Wrong Key
06-10 15:04:27.069: ERROR/wpa_supplicant(538): Set_key: Wrong Key
06-10 15:04:27.069: ERROR/wpa_supplicant(538): Set_key: Wrong Key
06-10 15:04:27.069: VERBOSE/WifiMonitor(72): Event [CTRL-EVENT-STATE-CHANGE id=0 state=8]
06-10 15:04:27.079: VERBOSE/WifiStateTracker(72): Changing supplicant state: COMPLETED ==> DORMANT
06-10 15:04:27.079: DEBUG/WifiStateTracker(72): Deconfiguring interface and stopping DHCP
06-10 15:04:27.099: VERBOSE/WifiMonitor(72): Event [CTRL-EVENT-DISCONNECTED - Disconnect event - remove keys]
06-10 15:04:27.099: VERBOSE/WifiMonitor(72): Event [CTRL-EVENT-STATE-CHANGE id=-1 state=8]
06-10 15:04:27.139: WARN/Smack/Packet(169): notify conn break (IOEx), close connection
06-10 15:04:27.139: DEBUG/Smack(169): [XMPPConn] close connection, notifyClosed=false
06-10 15:04:27.139: ERROR/MediaPlayer(390): error (1, -17)
06-10 15:04:27.139: ERROR/MediaPlayer(390): Error (1,-17)
06-10 15:04:28.109: VERBOSE/WifiMonitor(72): Event [CTRL-EVENT-DRIVER-STATE STOPPED]
06-10 15:04:28.129: VERBOSE/WifiStateTracker(72): New network state is DISCONNECTED
06-10 15:04:28.129: VERBOSE/WifiStateTracker(72): Changing supplicant state: DORMANT ==> DORMANT
06-10 15:04:28.189: INFO/MediaUploader(199): No need to wake up
06-10 15:04:28.189: DEBUG/GpsLocationProvider(72): updateNetworkState available
06-10 15:04:28.189: DEBUG/GpsLocationProvider(72): NetworkThread wait for 4484259ms
06-10 15:04:28.289: DEBUG/NetworkLocationProvider(72): onDataConnectionStateChanged 3
06-10 15:04:28.299: DEBUG/GpsLocationProvider(72): state: CONNECTING apnName: iinternet reason: null
06-10 15:04:32.979: DEBUG/NetworkLocationProvider(72): onDataConnectionStateChanged 3
06-10 15:04:33.029: DEBUG/GpsLocationProvider(72): state: CONNECTED apnName: iinternet reason: null
06-10 15:04:33.099: DEBUG/GpsLocationProvider(72): updateNetworkState available
06-10 15:04:33.099: DEBUG/GpsLocationProvider(72): NetworkThread wait for 4479355ms
06-10 15:04:33.129: INFO/MediaUploader(199): No need to wake up
06-10 15:04:33.299: INFO/ActivityManager(72): Stopping service: com.android.mms/.transaction.TransactionService
06-10 15:04:33.339: ERROR/TransactionSettings(156): Invalid APN setting: MMSC is empty
06-10 15:04:33.419: INFO/ActivityManager(72): Stopping service: com.android.providers.downloads/.DownloadService
06-10 15:04:38.309: DEBUG/dalvikvm(199): GC freed 301 objects / 19232 bytes in 92ms
06-10 15:04:43.349: DEBUG/dalvikvm(216): GC freed 1430 objects / 84920 bytes in 121ms
06-10 15:04:48.319: DEBUG/dalvikvm(156): GC freed 323 objects / 15152 bytes in 96ms
这是某种已知的错误/功能吗?如果是这样如何绕过它? 还有什么可以实际拦截这个EVENT然后soimehow忽略它或重新设置一个wifi连接? 提前致谢
答案 0 :(得分:4)
您需要创建一个wifi锁。这是你如何做到的:
WifiManager wifimanager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
WifiLock lock = wifimanager.createWifiLock("my_lock");
lock.acquire();
// your code here
lock.release();
请注意,当您主动使用wifi(即下载大文件)时,您应该只创建一个wifi锁,否则会不必要地影响电池续航时间。
答案 1 :(得分:0)
当我调用WifiLock.acquire()时,我得到一个SecurityException。我在清单中是否遗漏了使用许可,或者我需要事先在代码中检查一下吗? 我假设它与设置为NEVER的wifi设置页面中的高级设置有关。
答案 2 :(得分:0)
获取Wi-Fi锁定不是首选解决方案。 Wi-Fi锁定的目的是仅在您主动访问网络时保留它,即下载文件。例如。 Skype和Google HangOut不接受并保持Wi-Fi锁定。你应该做Martin Molnar在上面评论中所说的话。您应该让用户决定是否允许他的设备(以及他的设备上的应用程序)在显示器关闭时通过“睡眠时唤醒的WiFi”设置保持连接到Wi-Fi。如果你绝对坚持你的应用程序应该在显示器关闭后15分钟仍然保持Wi-Fi开启,即使用户将“睡眠中的WiFi唤醒”设置为“否”,那么你应该采取部分唤醒锁定。 Skype再次没有采取部分唤醒锁定。
如果您真的想要一个可以异步接收数据/通知的灵活应用程序,那么您应该设置GCM推送通知。