Switching between WiFi and GPRS networks in Android development

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.