Java 检查wifi是否在android类中打开
我有这门课:Java 检查wifi是否在android类中打开,java,android,android-studio,Java,Android,Android Studio,我有这门课: 导入android.content.Context; 导入android.net.wifi.WifiManager; 导入静态androidx.core.content.ContextCompat.getSystemService; 公共级Wifi{ 公共静态布尔值(){ WifiManager WifiManager=(WifiManager)getSystemService(Context.WIFI_服务); 返回wifiManager.isWifiEnabled(); }
导入android.content.Context;
导入android.net.wifi.WifiManager;
导入静态androidx.core.content.ContextCompat.getSystemService;
公共级Wifi{
公共静态布尔值(){
WifiManager WifiManager=(WifiManager)getSystemService(Context.WIFI_服务);
返回wifiManager.isWifiEnabled();
}
}
改用.getApplicationContext().getSystemService 而不是确切的哪一部分?WifiManager WifiManager=(WifiManager)getSystemService(Context.WIFI_SERVICE);=>WifiManager WifiManager=(WifiManager)getApplicationContext().getSystemService(Context.WIFI_服务);您可以像下面这样重写代码:public static boolean isOn(Context Context){WifiManager WifiManager=(WifiManager)Context.getSystemService(Context.WIFI_SERVICE);返回WifiManager.isWifiEnabled();},但当您在片段/活动中使用它时,请像下面这样使用WIFI.isOn(getApplicationContext())