The first two of the following methods are successfully tested in 2.3.5 and 4.1.1. the first two methods are normal in 2.3.5, but are invalid in 4.1.1. For details, see the Android. provider. settings. Secure class.
Remember to declare related permissions in androidmanifest. xml:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
1. The easiest way to enable WiFi is to directly call the system method:
/*** WiFi network switch */private void togglewifi (context, Boolean enabled) {wifimanager WM = (wifimanager) context. getsystemservice (context. wifi_service); WM. setwifienabled (Enabled );}
2. It is troublesome to open the mobile network. The system does not directly provide an open method. Only the connectivitymanager class has an invisible setmobiledataenabled method. Check the source code and find that it calls I Setmobiledataenabled (Boolean) method in the connectivitymanager class. Because the method is invisible, reflection can only be used for calling:
/*** Mobile network switch */private void togglemobiledata (context, Boolean enabled) {connectivitymanager conmgr = (connectivitymanager) Context. getsystemservice (context. connectivity_service); Class <?> Conmgrclass = NULL; // connectivitymanager class field iconmgrfield = NULL; // The Field object iconmgr = NULL in the connectivitymanager class; // reference class of the iconnectivitymanager class <?> Iconmgrclass = NULL; // iconnectivitymanager class method setmobiledataenabledmethod = NULL; // setmobiledataenabled method try {// get connectivitymanager class conmgrclass = Class. forname (conmgr. getclass (). getname (); // obtain the object mservice iconmgrfield = conmgrclass in the connectivitymanager class. getdeclaredfield ("mservice"); // you can set mservice to access iconmgrfield. setaccessible (true); // get the mservice instantiation class iconnectivitymanager iconmgr = iconmgrfield. get (conmgr); // get iconnectivitymanager class iconmgrclass = Class. forname (iconmgr. getclass (). getname (); // obtain the setmobiledataenabled (Boolean) method in the iconnectivitymanager class setmobiledataenabledmethod = iconmgrclass. getdeclaredmethod ("setmobiledataenabled", Boolean. type); // set the 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 ();}}
3. It is also troublesome to turn on GPS. Just like opening a mobile network, there is no direct method. After searching online, it is said that the method of calling the system through reflection will still fail. Some netizens have implemented the same function through another channel and can pass the test in the 2.3.5 system, the test in system 4.1.1 is invalid. to be resolved:
/*** <P> GPS switch * <p> turn on if off currently * <p> turn off if off currently */private void togglegps () {intent gpsintent = new intent (); gpsintent. setclassname ("com. android. settings "," com. android. settings. widget. settingsappwidgetprovider "); gpsintent. addcategory ("android. intent. category. alternative "); gpsintent. setdata (URI. parse ("custom: 3"); try {pendingintent. getbroadcast (this, 0, gpsintent, 0 ). send ();} catch (canceledexception e) {e. printstacktrace ();}}