Reprint: http://blog.csdn.net/tu_bingbing/article/details/8469871
In recent time due to the mobile phone network status to judge, open and close, from the Internet to find some information, now organized as follows
Includes WiFi, GPRS, flight mode on, off, and some status detection, on the Xiaomi and Samsung tablet test pass
<uses-permission android:name= "Android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name= "Android.permission.CHANGE_WIFI_STATE"/>
Packagecom.my.device_admin.business;ImportJava.lang.reflect.Method;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.net.ConnectivityManager;ImportAndroid.net.NetworkInfo;ImportAndroid.net.wifi.WifiManager;Importandroid.provider.Settings; Public classNetworkManager {Privatecontext Context; PrivateConnectivitymanager Connmanager; PublicNetworkManager (Context context) { This. Context =context; Connmanager= (Connectivitymanager) This. Context. Getsystemservice (Context.connectivity_service); } /** * @returnwhether the network connection is available*/ Public Booleanisnetworkconnected () {networkinfo networkinfo=Connmanager.getactivenetworkinfo (); if(Networkinfo! =NULL) { returnnetworkinfo.isconnected (); } return false; } /** * @returnWiFi connectivity is available*/ Public Booleaniswificonnected () {networkinfo Mwifi=Connmanager. Getnetworkinfo (Connectivitymanager.type_wifi); if(Mwifi! =NULL) { returnmwifi.isconnected (); } return false; } /*** Mobile does not work when WiFi does not reach the network *@returnwhether the GPRS connection is available*/ Public Booleanismobileconnected () {networkinfo mmobile=Connmanager. Getnetworkinfo (Connectivitymanager.type_mobile); if(Mmobile! =NULL) { returnmmobile.isconnected (); } return false; } /*** GPRS network switch Reflection Connectivitymanager Hide Method setmobiledataenabled can turn GPRS network on and off * *@paramisenable *@throwsException*/ Public voidTogglegprs (Booleanisenable)throwsException {Class<?> Cmclass =Connmanager.getclass (); Class<?>[] Argclasses =NewClass[1]; argclasses[0] =Boolean.class; //method to reflect hide in Connectivitymanager setmobiledataenabled, can turn GPRS network on and offmethod = Cmclass.getmethod ("setmobiledataenabled", argclasses); Method.invoke (Connmanager, isenable); } /*** WIFI network switch * *@paramenabled *@returnSet whether success*/ Public BooleanTogglewifi (Booleanenabled) {Wifimanager WM=(Wifimanager) context. Getsystemservice (Context.wifi_service); returnwm.setwifienabled (enabled); } /** * * @returnIs in airplane mode*/ Public BooleanIsairplanemodeon () {//a return value of 1 indicates in airplane mode intModeidx = Settings.System.getInt (Context.getcontentresolver (), Settings.System.AIRPLANE_MODE_ON, 0); BooleanIsEnabled = (Modeidx = = 1); returnisenabled; } /*** Flight Mode switch *@paramSetairplane*/ Public voidToggleairplanemode (BooleanSetairplane) {Settings.System.putInt (Context.getcontentresolver (), Settings.System.AIRPLANE_MODE_ON, Setairplane? 1:0); //broadcast flight mode signal changes, so that the corresponding program can be processed. //when not sending the broadcast, in non-flight mode, the Android 2.2.1 on the test turned off WiFi, does not turn off the normal call network (such as GMS/GPRS, etc.). //when the broadcast is not sent, in airplane mode, the test on Android 2.2.1 cannot turn off airplane mode. Intent Intent =NewIntent (intent.action_airplane_mode_changed); //Intent.putextra ("Sponsor", "Sodino"); //2.3 and later, you need to set this state, otherwise it will remain in the case of the operator disconnectedIntent.putextra ("state", Setairplane); Context.sendbroadcast (Intent); }}
[Android Traffic] Android Network on, off finishing