Caused by: java.lang.UnsupportedOperationException: Exceeded maximum number of wifi locks

问题如下:

 Caused by: java.lang.UnsupportedOperationException: Exceeded maximum number of wifi locks


问题原因:

先看代码如下:

WifiManager manager = (WifiManager) this
				.getSystemService(Context.WIFI_SERVICE);
WifiManager.MulticastLock lock= manager.createMulticastLock("test wifi");
lock.acquire();	//申请广播开启
lock.release();//释放

报错原因是:lock.acquire() 和 lock.release(),没有匹配对应使用.
即调用acquire()方法用完之后,没有及时调用release()方法。
若只调用acquire()方法,调用50次后程序必崩溃,报如下错误:

 E/AndroidRuntime(7742): FATAL EXCEPTION: Timer-0
 E/AndroidRuntime(7742): java.lang.UnsupportedOperationException: Exceeded maximum number of wifi locks
 E/AndroidRuntime(7742): at android.net.wifi.WifiManager$MulticastLock.acquire(WifiManager.java:1139)
 E/AndroidRuntime(7742): at sunny.fans.WifiTest$1$1.run(WifiTest.java:53)
 E/AndroidRuntime(7742): at java.util.Timer$TimerImpl.run(Timer.java:284)

测试三款手机,中兴、HTC及华为手机,调用50次acquire(),没有调用release()都会报如上错误。

小结:以后在调用WifiManager.MulticastLock来发送广播接收报文时,
记得acquire()方法和release()方法要匹配调用。

参考网址:
http://developer.android.com/reference/android/net/wifi/WifiManager.MulticastLock.html
http://blog.csdn.net/lvron/article/details/6606755

你可能感兴趣的:(Caused by: java.lang.UnsupportedOperationException: Exceeded maximum number of wifi locks)