Android studio编译错误二:Error: The WIFI_SERVICE must be looked up on the Application

来源:互联网 发布:软件开发cmm 编辑:程序博客网 时间:2024/05/29 17:47

编译错误

Error:Error: The WIFI_SERVICE must be looked up on the Application context or memory will leak on devices < Android N. Try changing  to .getApplicationContext()  [WifiManagerLeak]

原因

Android 7.0 在获取 WifiManager的时候需要使用.getApplicationContext(),如果未使用会造成内存泄露。

解决方法

原来的:

WifiManager wifiManager = (WifiManager)getSystemService(WIFI_SERVICE);

修改为:

WifiManager wifiManager = (WifiManager)getApplicationContext().getSystemService(WIFI_SERVICE);
阅读全文
1 0