Android Switch Data traffic

Source: Internet
Author: User

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?

  1. /**

  2. * GPRS status

  3. * @param getmobiledataenabled

  4. * @return

  5.      */

  6. Public boolean getmobiledatastatus (String getmobiledataenabled) {

  7. Connectivitymanager cm;

  8. CM = (Connectivitymanager)this. Getsystemservice (Context.connectivity_service);

  9. Class Cmclass = Cm.getclass ();

  10. class[] argclasses = null;

  11. object[] Argobject = null;

  12. Boolean IsOpen = false;

  13. Try {

  14. method = Cmclass.getmethod (getmobiledataenabled, argclasses);

  15. IsOpen = (Boolean) method.invoke (cm, argobject);

  16. }catch(Exception e) {

  17. E.printstacktrace ();

  18. }

  19. return IsOpen;

  20.     }  

/**     * 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?

  1. /**

  2. * Mobile network switch

  3.     */

  4. Private void setmobiledataenabled (context context, Boolean enabled) {

  5. Connectivitymanager conmgr = (connectivitymanager) context.getsystemservice (Context.connectivity_service);

  6. class<?> Conmgrclass = null; //Connectivitymanager class

  7. Field Iconmgrfield = null; fields in the//Connectivitymanager class

  8. Object iconmgr = null; //Iconnectivitymanager class reference

  9. class<?> Iconmgrclass = null; //Iconnectivitymanager class

  10. Method Setmobiledataenabledmethod = null; //Setmobiledataenabled method

  11. Try {

  12. //Get Connectivitymanager class

  13. Conmgrclass = Class.forName (Conmgr.getclass (). GetName ());

  14. //Get the object in the Connectivitymanager class Mservice

  15. Iconmgrfield = Conmgrclass.getdeclaredfield ("Mservice");

  16. //Set Mservice accessible

  17. Iconmgrfield.setaccessible (true);

  18. //Get Mservice instantiation class Iconnectivitymanager

  19. Iconmgr = Iconmgrfield.get (conmgr);

  20. //Get Iconnectivitymanager class

  21. Iconmgrclass = Class.forName (Iconmgr.getclass (). GetName ());

  22. //Get the Setmobiledataenabled (Boolean) method in the Iconnectivitymanager class

  23. Setmobiledataenabledmethod = Iconmgrclass.getdeclaredmethod ("setmobiledataenabled", Boolean.TYPE);

  24. //Set setmobiledataenabled method to access

  25. Setmobiledataenabledmethod.setaccessible (true);

  26. //Call the Setmobiledataenabled method

  27. Setmobiledataenabledmethod.invoke (iconmgr, enabled);

  28. } catch (ClassNotFoundException e) {

  29. E.printstacktrace ();

  30. } catch (Nosuchfieldexception e) {

  31. E.printstacktrace ();

  32. } catch (SecurityException e) {

  33. E.printstacktrace ();

  34. } catch (Nosuchmethodexception e) {

  35. E.printstacktrace ();

  36. } catch (IllegalArgumentException e) {

  37. E.printstacktrace ();

  38. } catch (Illegalaccessexception e) {

  39. E.printstacktrace ();

  40. } catch (InvocationTargetException e) {

  41. E.printstacktrace ();

  42. }

  43. }

 /**     *  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?

    1. 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?

  1. Private void Togglemobiledata (context context, Boolean enabled) {

  2. Connectivitymanager Connectivitymanager =

  3. (Connectivitymanager) Context.getsystemservice (Context.connectivity_service);

  4. Method setmobiledataenabl;

  5. Try {

  6. Setmobiledataenabl = Connectivitymanager.getclass (). Getdeclaredmethod ("setmobiledataenabled", Boolean. class);

  7. Setmobiledataenabl.invoke (Connectivitymanager, enabled);

  8. } catch (Exception e) {

  9. E.printstacktrace ();

  10. }

  11. }

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

Related Article

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.