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
[Java]View Plaincopy
- 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 Context;
- Private Connectivitymanager Connmanager;
- Public NetworkManager (Context context) {
- This . Context = context;
- Connmanager = (Connectivitymanager) this. Context
- . Getsystemservice (Context.connectivity_service);
- }
- /**
- * @return network connectivity is available
- */
- Public boolean isnetworkconnected () {
- Networkinfo networkinfo = Connmanager.getactivenetworkinfo ();
- if (networkinfo! = null) {
- return networkinfo.isconnected ();
- }
- return false;
- }
- /**
- * @return WiFi connection is available
- */
- Public boolean iswificonnected () {
- Networkinfo Mwifi = Connmanager
- . Getnetworkinfo (Connectivitymanager.type_wifi);
- if (Mwifi! = null) {
- return mwifi.isconnected ();
- }
- return false;
- }
- /**
- * Mobile only works when WiFi cannot access the network
- * @return Whether GPRS connection is available
- */
- Public boolean ismobileconnected () {
- Networkinfo Mmobile = Connmanager
- . Getnetworkinfo (Connectivitymanager.type_mobile);
- if (mmobile! = null) {
- return mmobile.isconnected ();
- }
- return false;
- }
- /**
- * GPRS network switch Reflection Connectivitymanager Hide Method setmobiledataenabled can turn GPRS network on and off
- *
- * @param isenable
- * @throws Exception
- */
- Public void Togglegprs (boolean isenable) throws Exception {
- class<?> Cmclass = Connmanager.getclass ();
- class<?>[] argclasses = new class[1];
- argclasses[0] = boolean. class ;
- //Reflection Connectivitymanager in Hide method setmobiledataenabled, can turn GPRS network on and off
- method = Cmclass.getmethod ("setmobiledataenabled", argclasses);
- Method.invoke (Connmanager, isenable);
- }
- /**
- * WiFi network switch
- *
- * @param enabled
- * @return Set whether success
- */
- Public boolean Togglewifi (boolean enabled) {
- Wifimanager wm = (Wifimanager) context
- . Getsystemservice (Context.wifi_service);
- return wm.setwifienabled (enabled);
- }
- /**
- *
- * @return is in airplane mode
- */
- Public boolean Isairplanemodeon () {
- //Return value is 1 indicates in airplane 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.putInt (Context.getcontentresolver (), Settings.System.AIRPLANE_MODE_ON, Setairplane? 1 : 0 );
- //Broadcast flight mode signal changes, so that the corresponding program can be processed.
- //Do not send the broadcast, in non-flight mode, the Android 2.2.1 on the test turned off WiFi, do not turn off the normal call network (such as GMS/GPRS, etc.).
- //Do not send the broadcast when in airplane mode, the Android 2.2.1 on the test cannot turn off airplane mode.
- Intent Intent = new Intent (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 disconnected
- Intent.putextra ("state", Setairplane);
- Context.sendbroadcast (Intent);
- }
- }