我写了一些软件,使用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

这是某种已知的bug/特性吗?如果是这样的话,怎么绕过它呢?
还有什么方法可以截获此事件,然后忽略它或重新设置WiFi连接?
提前谢谢

最佳答案

你需要创建一个wifi锁。以下是您的操作方法:

WifiManager wifimanager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
WifiLock lock = wifimanager.createWifiLock("my_lock");

lock.acquire();

// your code here

lock.release();

请注意,只有在积极使用WiFi(即下载大文件)时,才应创建WiFi锁,否则将不必要地影响电池寿命。

关于android - 无线上网进入闲置状态,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3014699/

10-16 21:20