Skip to content

Instantly share code, notes, and snippets.

@alximw
Last active September 4, 2018 10:30
Show Gist options
  • Select an option

  • Save alximw/32b99f8d9e54229af86344fec405d3d0 to your computer and use it in GitHub Desktop.

Select an option

Save alximw/32b99f8d9e54229af86344fec405d3d0 to your computer and use it in GitHub Desktop.
public void setMobileDataEnabled(boolean mobileDataEnabled) {
try {
Object connectivityManager = this.ctx.getSystemService("connectivity");
Field mServiceField = Class.forName(connectivityManager.getClass().getName()).getDeclaredField("mService");
mServiceField.setAccessible(true);
connectivityManager = mServiceField.get(connectivityManager);
Method setMobileDataEnabledMethod = Class.forName(connectivityManager.getClass().getName()).getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(connectivityManager, Boolean.valueOf(mobileDataEnabled));
}
catch(Exception ex) {
ex.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment