android - Enable/Disable Data mobile programmatically - NO ROOT -


i'm trying enable/disable data mobile in specific moments. i've tried on motog 4.4.2 (rooted) had no problem this, when i've tried on samsung galaxy grand prime 4.4.4 (no root) doesn't work, i've tried on samsung galaxy s3 4.3 (no root) worked.

  1. motog worked -- why? -- root acces
  2. samsung galaxy grand prime didn't work -- why? -- think doesn't work because when try manually i've accept dialog want lose data connectivity, , think problem because doesn't work.
  3. samsung galaxy s3 worked -- why? -- think worked because user put "don't show me again" , user accepted before.

the code i'm trying :

public void getdataconnectionapi() {     this.context.getapplicationcontext();     telephonymanager telephonymanager =             (telephonymanager) this.context.getapplicationcontext().                     getsystemservice(context.telephony_service);     try {         telephonymanagerclass = class.forname(telephonymanager.getclass().getname());         method getitelephonymethod = telephonymanagerclass.getdeclaredmethod("getitelephony");         getitelephonymethod.setaccessible(true);         itelephonystub = getitelephonymethod.invoke(telephonymanager);         itelephonyclass = class.forname(itelephonystub.getclass().getname());          dataconnswitchmethod_off =                 itelephonyclass.getdeclaredmethod("disabledataconnectivity");         dataconnswitchmethod_on = itelephonyclass.getdeclaredmethod("enabledataconnectivity");     } catch (exception e) {          e.printstacktrace();     } } 

and enable/disable data method :

public void setmobiledataenabled(context context, boolean enabled) throws classnotfoundexception, nosuchfieldexception, illegalaccessexception, nosuchmethodexception, invocationtargetexception {     final connectivitymanager conman = (connectivitymanager)  context.getsystemservice(context.connectivity_service);     final class conmanclass = class.forname(conman.getclass().getname());     final field connectivitymanagerfield = conmanclass.getdeclaredfield("mservice");     connectivitymanagerfield.setaccessible(true);     final object connectivitymanager = connectivitymanagerfield.get(conman);     final class connectivitymanagerclass =  class.forname(connectivitymanager.getclass().getname());     final method setmobiledataenabledmethod = connectivitymanagerclass.getdeclaredmethod("setmobiledataenabled", boolean.type);     setmobiledataenabledmethod.setaccessible(true);      setmobiledataenabledmethod.invoke(connectivitymanager, enabled);  } 

when want enable call method :

public void enablemobiledata(){     try {         setmobiledataenabled(context, true);     } catch (classnotfoundexception e) {         e.printstacktrace();     } catch (nosuchfieldexception e) {         e.printstacktrace();     } catch (illegalaccessexception e) {         e.printstacktrace();     } catch (nosuchmethodexception e) {         e.printstacktrace();     } catch (invocationtargetexception e) {         e.printstacktrace();     } } 

on manifest put

<!-- 3g uses-permisions--> <uses-permission android:name="android.permission.change_network_state"/> <uses-permission android:name="android.permission.access_network_state" /> <uses-permission android:name="android.permission.modify_phone_state" /> 

i wonder if it's possibility avoid dialog or accept programmatically..

i've read bojan kogoj answer sais i've :

  • pre-installed system folder on rom
  • compiled manufacturer using security certificate

but i'm wondering if other way this.

the modify_phone_state permission not guaranteed work in cases. default system-level permission, work on devices manufacturers have enabled it.

similarly, methods getitelephony() device/manufacturer-specific, , there no way name of methods doing on every device.

unfortunately there no guaranteed way this.


Comments