How can i turn off 3G/Data programmatically on Android?
There is no official way to do this. However, it can be achieved unofficially with reflection.
For Android 2.3 and above:
private void setMobileDataEnabled(Context context, boolean enabled) {
final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final Class conmanClass = Class.forName(conman.getClass().getName());
final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
final Object iConnectivityManager = iConnectivityManagerField.get(conman);
final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
}
This also requires the following permission.
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
For Android 2.2 and below:
Method dataConnSwitchmethod;
Class telephonyManagerClass;
Object ITelephonyStub;
Class ITelephonyClass;
TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
if(telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED){
isEnabled = true;
}else{
isEnabled = false;
}
telephonyManagerClass = Class.forName(telephonyManager.getClass().getName());
Method getITelephonyMethod = telephonyManagerClass.getDeclaredMethod("getITelephony");
getITelephonyMethod.setAccessible(true);
ITelephonyStub = getITelephonyMethod.invoke(telephonyManager);
ITelephonyClass = Class.forName(ITelephonyStub.getClass().getName());
if (isEnabled) {
dataConnSwitchmethod = ITelephonyClass
.getDeclaredMethod("disableDataConnectivity");
} else {
dataConnSwitchmethod = ITelephonyClass
.getDeclaredMethod("enableDataConnectivity");
}
dataConnSwitchmethod.setAccessible(true);
dataConnSwitchmethod.invoke(ITelephonyStub);
This required the following permission:
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
Note that both of these are unofficial and may no longer work. No more proof of this kind of thing breaking should be needed, as the 2.2 and below method broke on 2.3.
How to turn off 3G data connection programmatically in android?
This code sample should work for android phones running gingerbread and higher:
private void setMobileDataEnabled(Context context, boolean enabled)
{
final ConnectivityManager conman = (ConnectivityManager)
final ConnectivityManager conman = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
final Class conmanClass = Class.forName(conman.getClass().getName());
final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
final Object iConnectivityManager = iConnectivityManagerField.get(conman);
final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
}
This also requires the following permission.
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
How can I turn off/on 3G/4G/Data programmatically on Android 5.0?
Google has called off their API for data on/off pragmatically.
You can check the issue filed on link.
If you want to know some more details long answer is on this link where detailed discussion is there
and if you find this helpful accept this as an answer
How to enable mobile data on/off programmatically
You cannot access mobile data on / off programmaticaly above android 4.4 .It have been made stopped for security reasons ,instead you can ask the user using a dialog to enable the mobile data and then if he enable you can do your task.
Turn on/off mobile data in Android - NO REFLECTION
Android does not supply an API method for disabling mobile data. So reflection is needed to achieve this.
Here is a Stack Overflow thread that explains better
How to enable/disable 3g data service in Android?
Thanks to stackoverflow.com
found answer: How to disable Mobile Data on Android
Method dataConnSwitchmethod;
Class telephonyManagerClass;
Object ITelephonyStub;
Class ITelephonyClass;
TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
if(telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED){
isEnabled = true;
}else{
isEnabled = false;
}
telephonyManagerClass = Class.forName(telephonyManager.getClass().getName());
Method getITelephonyMethod = telephonyManagerClass.getDeclaredMethod("getITelephony");
getITelephonyMethod.setAccessible(true);
ITelephonyStub = getITelephonyMethod.invoke(telephonyManager);
ITelephonyClass = Class.forName(ITelephonyStub.getClass().getName());
if (isEnabled) {
dataConnSwitchmethod = ITelephonyClass
.getDeclaredMethod("disableDataConnectivity");
} else {
dataConnSwitchmethod = ITelephonyClass
.getDeclaredMethod("enableDataConnectivity");
}
dataConnSwitchmethod.setAccessible(true);
dataConnSwitchmethod.invoke(ITelephonyStub);
Related Topics
How to Set Entire Application in Portrait Mode Only
Display the Current Time and Date in an Android Application
Notification Click: Activity Already Open
Ffmpeg for a Android (Using Tutorial: "Ffmpeg and Android.Mk")
Listview Subobject Clickable Confilct
How to "Select Android Sdk" in Android Studio
How to Prevent Android Bluetooth Rfcomm Connection from Dying Immediately After .Connect()
Quality Problems When Resizing an Image at Runtime
Get Altitude by Longitude and Latitude in Android
Android App Specific Soft Keyboard
Update Fragment from Viewpager
Android Appcompat 21 Elevation
Get Database File in /Data/Data on Rooted Device