Mobile network switch of Android5.0 above system

Source: Internet
Author: User

I recently encountered a very interesting bug, posted out and share with you.
It was a warm morning, and the sun was very comfortable for the people. A bug e-mail like a leaf floated to my mailbox.

Communication. The author confirms that the responsible widget switch has no effect on the system above Android5.0. I believe a lot of friends who have done mobile network switches know. The traditional method is to control the mobile network on and off by reflecting two methods setmobiledataenabled and getmobiledataenabled in Connectivitymanager.

    /** * Gets The value of the setting for enabling Mobile data.     * * @return Whether mobile data is enabled. * @hide * *     Public Boolean getmobiledataenabled() {Try{returnMservice.getmobiledataenabled (); }Catch(RemoteException e) {return true; }    }/** * Sets the persisted value for enabling/disabling Mobile data.     * * @param enabled Whether the mobile data connection should is * used or not. * @hide * *     Public void setmobiledataenabled(BooleanEnabled) {Try{mservice.setmobiledataenabled (enabled); }Catch(RemoteException e) {        }    }

But open more than 5.0 of the source code, these two methods no longer exist.
Recommend a good online look at the source code of the site. Online source code URL.

At this time the boss's reminder helped me. I opened the Telephonymananger class with more than 5.0 codes trying to get the setdataenabled and getdataenabled classes done through reflection.
Source

/** @hide * /    @SystemApi     Public void setdataenabled(BooleanEnable) {Try{Getitelephony (). setdataenabled (enable); }Catch(RemoteException e) {LOG.E (TAG,"Error calling itelephony#setdataenabled", e); }    }/** @hide * /    @SystemApi     Public Boolean getdataenabled() {Try{returnGetitelephony (). getdataenabled (); }Catch(RemoteException e) {LOG.E (TAG,"Error calling itelephony#getdataenabled", e); }return false; }

My method of Reflection:

 Public void setmobiledatastate(Context CXT,Booleanmobiledataenabled) {Telephonymanager Telephonyservice = (telephonymanager) cxt.getsystemservice (Context.TELEPHONY_ SERVICE);Try{Method Setmobiledataenabledmethod = Telephonyservice.getclass (). Getdeclaredmethod ("Setdataenabled",Boolean. Class);if(NULL! = Setmobiledataenabledmethod) {Setmobiledataenabledmethod.invoke (Telephonyservice, mobileDataEnabled); }    }Catch(Exception e) {LOGHELPER.V (TAG,"Error setting"+ ((invocationtargetexception) e). Gettargetexception () + Telephonyservice); }} Public Boolean getmobiledatastate(Context cxt) {Telephonymanager Telephonyservice = (telephonymanager) cxt.getsystemservice (Context.telephony_service);Try{Method Getmobiledataenabledmethod = Telephonyservice.getclass (). Getdeclaredmethod ("Getdataenabled");if(NULL! = Getmobiledataenabledmethod) {Booleanmobiledataenabled = (Boolean) getmobiledataenabledmethod.invoke (Telephonyservice);returnmobiledataenabled; }    }Catch(Exception e) {LOGHELPER.V (TAG,"Error getting"+ ((invocationtargetexception) e). Gettargetexception () + Telephonyservice); }return false;}

But the invocationtargetexception error was reported.

Pass:

((InvocationTargetException)e).getTargetException()

The method is known to be running the reflection runtime, such as the following error:

10240 nor current process has android.permission.MODIFY_PHONE_STATE.android.telephony.TelephonyManager

This time the boss reminds me to move the app to System/app and try it.


Several attempts. is still the same result.

This time one of my unintentional operation, the app moved to System/priv-app, incredibly successful. In this folder, the switch can work again.

Of course, our application cannot be installed in this folder, only to take a compromise solution. But the whole process is very happy, this bug is the first time I found the different versions of the source code between Android, the feeling is very interesting. I hope we have some inspiration.

Mobile network switch of Android5.0 above system

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.