简体   繁体   English

检查 wifi 是否在 android 的类中

[英]Check if wifi is on or not inside a class in android

I have this class:我有这门课:

 import android.content.Context; import android.net.wifi.WifiManager; import static androidx.core.content.ContextCompat.getSystemService; public class Wifi { public static boolean isOn() { WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); return wifiManager.isWifiEnabled(); } }

I want to return the state of wifi using this class and method.我想使用这个类和方法返回wifi的状态。 But I get an error for getSystemService(Context.WIFI_SERVICE) part:但是我收到了getSystemService(Context.WIFI_SERVICE)部分的错误:

Error: The WIFI_SERVICE must be looked up on the Application context or memory will leak on devices

使用 .getApplicationContext().getSystemService 代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM