1. 程式人生 > >Android studio編譯錯誤:Error: The WIFI_SERVICE must be looked up on the Application

Android studio編譯錯誤:Error: The WIFI_SERVICE must be looked up on the Application

編譯錯誤

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]
  • 1

原因

Android 7.0 在獲取 WifiManager的時候需要使用.getApplicationContext(),如果未使用會造成記憶體洩露。

解決方法

原來的:

WifiManager wifiManager = (WifiManager)getSystemService(WIFI_SERVICE);
  • 1

修改為:

WifiManager wifiManager = (WifiManager)getApplicationContext().getSystemService(WIFI_SERVICE);