Although this is a "duplicate", the current answers are out of date and, mostly, no longer apply. I thought it would be good to provide an updated resource here, if possible, to save people time, as I have just done, researching this issue.
I've been googling around to see the latest information on being able to enable and disable mobile data from within an app (if wifi is not available).
This is one of the latest things I can find:
Did you know you can no longer Disable/Enable Data on lollipop from a widget?
There is an answer to that, I quote:
There was never an API for it. Developers were using a workaround by calling the method via Reflections. All Google did was close this "exploit".
There is also this discussion:
Replacement for setMobileDataEnabled() api
Which is Feb 2015.
There are these questions here:
How to disable Mobile Data on Android
This was asked in 2010 and the latest answer was updated with a one liner on Dec 2014.
Enable/disable data connection in android programmatically
And this, the accepted answer in 2012.
What's the latest on this issue?
Can it still be done?
It wont work on non-rooted phone as they added MODIFY_PHONE_STATE
permission check. This permission is only given to system or signature apps refer-here.
Check the code below from PhoneInterfaceManager:
@Override
public void setDataEnabled(boolean enable) {
enforceModifyPermission();
mPhone.setDataEnabled(enable);
}
private void enforceModifyPermission() {
mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
}