android - Latest update on enabling and disabling mobile data programmatically -


although "duplicate", current answers out of date and, mostly, no longer apply. thought provide updated resource here, if possible, save people time, have done, researching issue.

i've been googling around see latest information on being able enable , disable mobile data within app (if wifi not available).

this 1 of latest things can find:
did know can no longer disable/enable data on lollipop widget?

there answer that, quote:

there never api it. developers using workaround calling method via reflections. google did close "exploit".

there discussion:

replacement setmobiledataenabled() api
feb 2015.

there these questions here:

how disable mobile data on android

this asked in 2010 , latest answer updated 1 liner on dec 2014.

enable/disable data connection in android programmatically

and this, accepted answer in 2012.

what's latest on issue?

can still done?

i use workaround only works rooted phones.

the setmobiledataenabled method removed connectivitymanager 2 methods getdataenabled , setdataenabled added telephonymanager functionality.

public void setmobiledatastate(boolean mobiledataenabled) {     try     {         telephonymanager telephonyservice = (telephonymanager) getsystemservice(context.telephony_service);          method setmobiledataenabledmethod = telephonyservice.getclass().getdeclaredmethod("setdataenabled", boolean.class);          if (null != setmobiledataenabledmethod)         {             setmobiledataenabledmethod.invoke(telephonyservice, mobiledataenabled);         }     }     catch (exception ex)     {         log.e(tag, "error setting mobile data state", ex);     } }  public boolean getmobiledatastate() {     try     {         telephonymanager telephonyservice = (telephonymanager) getsystemservice(context.telephony_service);          method getmobiledataenabledmethod = telephonyservice.getclass().getdeclaredmethod("getdataenabled");          if (null != getmobiledataenabledmethod)         {             boolean mobiledataenabled = (boolean) getmobiledataenabledmethod.invoke(telephonyservice);              return mobiledataenabled;         }     }     catch (exception ex)     {         log.e(tag, "error getting mobile data state", ex);     }      return false; } 

but need add permission (modify_phone_state) manifest file otherwise securityexception.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -