In recent times, due to the need to determine, enable, and disable the network conditions of the mobile phone, I found some information from the Internet, which is as follows:
Including the activation and shutdown of WiFi, GPRS, and flight modes, as well as the detection of some statuses, all of which were tested on Xiaomi and Samsung tablets.
Package COM. my. device_admin.business; import Java. lang. reflect. method; import android. content. context; import android. content. intent; import android.net. connectivitymanager; import android.net. networkinfo; import android.net. wiFi. wifimanager; import android. provider. settings; public class NetworkManager {private context; private connectivitymanager connmanager; Public NetworkManager (context) {This. context = context; connmanager = (connectivitymanager) This. context. getsystemservice (context. connectivity_service);}/*** @ return whether the network connection is available */Public Boolean isnetworkconnected () {networkinfo = connmanager. getactivenetworkinfo (); If (networkinfo! = NULL) {return networkinfo. isconnected ();} return false;}/*** @ return WiFi connection available */Public Boolean iswificonnected () {networkinfo mwifi = connmanager. getnetworkinfo (connectivitymanager. type_wifi); If (mwifi! = NULL) {return mwifi. isconnected ();} return false;}/*** mobile works only when WiFi cannot access the network * @ return GPRS connection availability */Public Boolean ismobileconnected () {networkinfo mmobile = connmanager. getnetworkinfo (connectivitymanager. type_mobile); If (mmobile! = NULL) {return mmobile. isconnected ();} return false ;} /*** call the setmobiledataenabled method of hide in connectivitymanager to enable or disable the GPRS network *** @ Param isenable * @ throws exception */Public void togglegprs (Boolean isenable) throws exception {class <?> Cmclass = connmanager. getclass (); Class <?> [] Argclasses = new class [1]; argclasses [0] = Boolean. class; // reflect the hide method setmobiledataenabled in connectivitymanager. You can enable or disable the GPRS network method = cmclass. getmethod ("setmobiledataenabled", argclasses); method. invoke (connmanager, isenable);}/*** WiFi network switch ** @ Param enabled * @ return sets whether success */Public Boolean togglewifi (Boolean enabled) {wifimanager WM = (wifimanager) context. getsystemservice (context. wi Fi_service); Return WM. setwifienabled (Enabled);}/***** @ return whether it is in flight mode */Public Boolean isairplanemodeon () {// when the return value is 1, it indicates that it is in flight mode int modeidx = settings. system. getint (context. getcontentresolver (), settings. system. airplane_mode_on, 0); Boolean isenabled = (modeidx = 1); Return isenabled;}/*** flight mode switch * @ Param setairplane */Public void toggleairplanemode (Boolean setairplane) {settings. system. pu Tint (context. getcontentresolver (), settings. system. airplane_mode_on, setairplane? 1: 0); // Changes the signal of the broadcast flight mode so that the corresponding program can be processed. // When broadcasting is not sent, in non-flight mode, WiFi is disabled in Android 2.2.1 and normal call networks (such as GMS/GPRS) are not closed ). // When broadcasting is not sent, the flight mode cannot be closed during testing on Android 2.2.1 in flight mode. Intent intent = new intent (intent. action_airplane_mode_changed); // intent. putextra ("container sor", "sodino"); // 2.3 and later, you need to set this status. Otherwise, intent will remain disconnected from the carrier. putextra ("State", setairplane); context. sendbroadcast (intent );}}