I recently encountered a very interesting bug, post it and share it with you.
It was a warm morning, and the sun was very comfortable. A bug e-mail like a leaf floated to my mailbox.
A communication, I confirm that the responsible widget switch in Android5.0 above the system has no effect. Believe that many have done mobile network switch friends know that the traditional method is in the Connectivitymanager by reflection two methods setmobiledataenabled and getmobiledataenabled to control the mobile network open and close.
/** * 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 to see the source of the website. Online source URL.
At this time the boss's reminder helped me, I opened more than 5.0 code of the Telephonymananger class try to get setdataenabled and getdataenabled class through reflection to complete the operation.
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 reported a invocationtargetexception error. Pass:
((InvocationTargetException)e).getTargetException()
method to know that the following error occurred while performing reflection execution:
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.
A few attempts are still the same result.
This time one of my unintentional operation, the app moved to System/priv-app, unexpectedly succeeded. In this directory, the switch can be re-working again.
Of course, our application can not be installed in this directory, only to take a compromise solution. But the whole process is still very happy, this bug is the first time I found the different versions of the source code between Android, it feels very interesting. I hope we have some inspiration.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Mobile network switch of Android5.0 above system