After looking for a long time on the internet, we found a reflection method to reflect the Setmobiledataenabled method of an object Iconnectivitymanager in Connectivitymanager. Because Iconnectivitymanager is private, you cannot call setmobiledataenabled directly.
This method, the specific code is as follows:
[Java] view Plaincopyprint?
/**
* GPRS status
* @param getmobiledataenabled
* @return
*/
Public boolean getmobiledatastatus (String getmobiledataenabled) {
Connectivitymanager cm;
CM = (Connectivitymanager)this. Getsystemservice (Context.connectivity_service);
Class Cmclass = Cm.getclass ();
class[] argclasses = null;
object[] Argobject = null;
Boolean IsOpen = false;
Try {
method = Cmclass.getmethod (getmobiledataenabled, argclasses);
IsOpen = (Boolean) method.invoke (cm, argobject);
}catch(Exception e) {
E.printstacktrace ();
}
return IsOpen;
-
}
/** * gprs * @param getmobiledataenabled * @return */ public boolean getmobiledatastatus (string getmobiledataenabled) { ConnectivityManager cm; cm = ( Connectivitymanager) This.getsystemservice (context.connectivity_service); class cmclass = cm.getclass (); Class[] argClasses = null; object[] argobject = null; boolean isopen = false; try{ method mEthod = cmclass.getmethod (getmobiledataenabled, argclasses); isOpen = (Boolean) Method.invoke (cm, argobject); }catch (exception e) { E.printstacktrace (); } return isopen; }
[Java] view Plaincopyprint?
/**
* Mobile network switch
*/
Private void setmobiledataenabled (context context, Boolean enabled) {
Connectivitymanager conmgr = (connectivitymanager) context.getsystemservice (Context.connectivity_service);
class<?> Conmgrclass = null; //Connectivitymanager class
Field Iconmgrfield = null; fields in the//Connectivitymanager class
Object iconmgr = null; //Iconnectivitymanager class reference
class<?> Iconmgrclass = null; //Iconnectivitymanager class
Method Setmobiledataenabledmethod = null; //Setmobiledataenabled method
Try {
//Get Connectivitymanager class
Conmgrclass = Class.forName (Conmgr.getclass (). GetName ());
//Get the object in the Connectivitymanager class Mservice
Iconmgrfield = Conmgrclass.getdeclaredfield ("Mservice");
//Set Mservice accessible
Iconmgrfield.setaccessible (true);
//Get Mservice instantiation class Iconnectivitymanager
Iconmgr = Iconmgrfield.get (conmgr);
//Get Iconnectivitymanager class
Iconmgrclass = Class.forName (Iconmgr.getclass (). GetName ());
//Get 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 ();
}
}
/** * Mobile network Switches */ Private void setmobiledataenabled (context context, boolean enabled) { connectivitymanager conmgr = (Connectivitymanager) context.getsystemservice ( Context.connectivity_service); class<?> conmgrclass = null; // Fields in the Connectivitymanager class field iconmgrfield = null; // connectivitymanager class object iconmgr = null; // iconnectivitymanager class References Class<?> Iconmgrclass = null; // iconnectivitymanager class method setmobiledataenabledmethod = null; // setmobiledataenabled Methods try { // Obtained Connectivitymanager class conmgrclass = class.forname (Conmgr.getclass (). GetName ()); // get objects in the Connectivitymanager class MSERVICE&NBSP;&NBSP;ICONMGRFIELD&NBsp;= conmgrclass.getdeclaredfield ("Mservice"); // settings Mservice accessible Iconmgrfield.setaccessible (True); // gets the instantiated class of Mservice Iconnectivitymanager iconmgr = iconmgrfield.get (conmgr); // acquired Iconnectivitymanager class iconmgrclass = class.forname (Iconmgr.getclass (). GetName ()); // Get 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 (); } }
This error has been found for half a day ~ ~ ~ didn't find a way to solve
[HTML] view Plaincopyprint?
java.lang.NoSuchMethodException:setMobileDataEnabled [Boolean]
java.lang.NoSuchMethodException:setMobileDataEnabled [Boolean]
Finally found a way to try to find out on the internet
[Java] view Plaincopyprint?
Private void Togglemobiledata (context context, Boolean enabled) {
Connectivitymanager Connectivitymanager =
(Connectivitymanager) Context.getsystemservice (Context.connectivity_service);
Method setmobiledataenabl;
Try {
Setmobiledataenabl = Connectivitymanager.getclass (). Getdeclaredmethod ("setmobiledataenabled", Boolean. class);
Setmobiledataenabl.invoke (Connectivitymanager, enabled);
} catch (Exception e) {
E.printstacktrace ();
}
}
private void Togglemobiledata (context context, Boolean enabled) {Connectivitymanager Connectivitymanager = (connectiv Itymanager) Context.getsystemservice (Context.connectivity_service); Method setmobiledataenabl;try {setmobiledataenabl = Connectivitymanager.getclass (). Getdeclaredmethod (" Setmobiledataenabled ", Boolean.class); Setmobiledataenabl.invoke (Connectivitymanager, enabled);} catch (Exception e) {e.printstacktrace ();}}
Android Switch Data traffic