In the development of the project because of the use of WiFi and GPRS network switching, so the study of the code to open WiFi and GPRS work.
The following lines of code need to be added to the Androidmanifest.xml file, whether you need to set the appropriate permissions for switching WiFi or switching the GPRS network.
<uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE"/> <uses-permission android: Name= "Android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name= " Android.permission.CHANGE_WIFI_STATE "/> <uses-permission android:name=" android.permission.CHANGE_ Network_state "/>
1. Switch WiFi network
public static void Togglewifi (context context, Boolean enabled) {Wifimanager wm = (Wifimanager) context.getsystemservice ( Context.wifi_service); wm.setwifienabled (enabled);}
2. Switch GPRS network
Since Android does not provide a direct way to switch the GPRS network, by viewing the system source discovery, the system is called Iconnectivitymanager Class Setmobiledataenabled (Boolean) method to set up the GPRS network, Because the method is not visible, it can only be called with reflection, the code is as follows.
public static void Togglemobiledata (context context, Boolean enabled) {Connectivitymanager Conmgr = (connectivitymanager ) Context.getsystemservice (Context.connectivity_service); Class<?> conmgrclass = null; Connectivitymanager class field conmgrfield = null; field in the Connectivitymanager class object iconmgr = null; The reference to the Iconnectivitymanager class class<?> iconmgrclass = null; Iconnectivitymanager class method setmobiledataenabledmethod = null; Setmobiledataenabled method try {//get Connectivitymanager class Conmgrclass = Class.forName (Conmgr.getclass (). GetName ());// Gets the object in the Connectivitymanager class Mserviceconmgrfield = Conmgrclass.getdeclaredfield ("Mservice");// Set Mservice to access conmgrfield.setaccessible (TRUE);//Get mservice instantiation class Iconnectivitymanagericonmgr = Conmgrfield.get ( CONMGR);//Get Iconnectivitymanager class Iconmgrclass = Class.forName (Iconmgr.getclass (). GetName ());// Gets the Setmobiledataenabled (Boolean) method in the Iconnectivitymanager class Setmobiledataenabledmethod = Iconmgrclass.getdeclaredmethod ("setmobiledataenabled", Boolean.TYPE);//Set setmobiledataenabled method to access setmobiledataenabledmethod.setaccessible (TRUE);// Call the Setmobiledataenabled method Setmobiledataenabledmethod.invoke (iconmgr, enabled);} catch (ClassNotFoundException e) {e.printstacktrace ();} catch (Nosuchfieldexception e) {e.printstacktrace ();} catch (SecurityException e) {e.printstacktrace ();} catch (Nosuchmethodexception e) {e.printstacktrace ();} catch (IllegalArgumentException e) {e.printstacktrace ();} catch (Illegalaccessexception e) {e.printstacktrace ();} catch (InvocationTargetException e) {e.printstacktrace ();}}
According to the above write can do WiFi network and GPRS network switch.
Switching between WiFi and GPRS networks in Android development