How to Disable Mobile Data on Android

How to disable Mobile Data on Android

The Dataconnection disable and enabling APIS are hidden in the SDK and not exposed to the user, this can be achived by accessing the ITelephony interface using the java reflection technique.

here you go:

    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);

Prevent user from turning off mobile data and location

No, you can't do that.

Even if you could, it would not solve anything, because there are many other reasons why those services may not work. For example when one is driving through a tunnel and there is no signal or out of town there may sometimes be no signal or the network may be overloaded or something. So you can't rely on these services anyway. Intermittent loss of connectivity is pretty common in mobile devices. You will have to take it into account (log data and send them when you succeed in reconnecting).

If the users need the application, and connectivity in it, to do their job, they won't be turning it off, because they need it. So all that is needed is to create a notification when connectivity and/or location is lost so the user can do something about it if:

  • the application fails to connect when they turn it on at the start of their shift or
  • the application looses connection for extended period of time for whatever reason.

And of course if there is somebody in the company using the data (some dispatcher), which I suppose there is, they will notice the particular worker is not sending anything and will try to contact them by calling or something. These should take care of mistakes and faults.

If the users don't need it and it is intended to spy on them (so they may have a reason to disable it), it is probably illegal anyway.

How to disable Mobile Data on Android with Xamarin

Typically Xamarin uses the same methodology as Java when interacting with Android. Therefore its best to just port over the equivalent Java code to C#; in this case translating the reflection code in the answer you linked to.

Here is a port that combines Gingerbread and higher support and Froyo and lower support:

void SetMobileDataEnabled(bool enabled)
{
if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.L) {
Console.WriteLine ("Device does not support mobile data toggling.");
return;
}

try {
if (Build.VERSION.SdkInt <= Android.OS.BuildVersionCodes.KitkatWatch
&& Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Gingerbread) {
Android.Net.ConnectivityManager conman = (Android.Net.ConnectivityManager)GetSystemService (ConnectivityService);
Java.Lang.Class conmanClass = Java.Lang.Class.ForName (conman.Class.Name);
Java.Lang.Reflect.Field iConnectivityManagerField = conmanClass.GetDeclaredField ("mService");
iConnectivityManagerField.Accessible = true;
Java.Lang.Object iConnectivityManager = iConnectivityManagerField.Get (conman);
Java.Lang.Class iConnectivityManagerClass = Java.Lang.Class.ForName (iConnectivityManager.Class.Name);
Java.Lang.Reflect.Method setMobileDataEnabledMethod = iConnectivityManagerClass.GetDeclaredMethod ("setMobileDataEnabled", Java.Lang.Boolean.Type);
setMobileDataEnabledMethod.Accessible = true;

setMobileDataEnabledMethod.Invoke (iConnectivityManager, enabled);
}

if (Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.Gingerbread) {

TelephonyManager tm = (TelephonyManager)GetSystemService (Context.TelephonyService);

Java.Lang.Class telephonyClass = Java.Lang.Class.ForName (tm.Class.Name);
Java.Lang.Reflect.Method getITelephonyMethod = telephonyClass.GetDeclaredMethod ("getITelephony");
getITelephonyMethod.Accessible = true;

Java.Lang.Object stub = getITelephonyMethod.Invoke (tm);
Java.Lang.Class ITelephonyClass = Java.Lang.Class.ForName (stub.Class.Name);

Java.Lang.Reflect.Method dataConnSwitchMethod = null;
if (enabled) {
dataConnSwitchMethod = ITelephonyClass
.GetDeclaredMethod ("disableDataConnectivity");
} else {
dataConnSwitchMethod = ITelephonyClass
.GetDeclaredMethod ("enableDataConnectivity");
}

dataConnSwitchMethod.Accessible = true;
dataConnSwitchMethod.Invoke (stub);
}
} catch (Exception ex) {
Console.WriteLine ("Device does not support mobile data toggling.");
}
}

Enable the ChangeNetworkState and ModifyPhoneState permissions in your manifest.

Android L currently has no available way to disable/enable mobile data.

Disable/Enabling mobile data programmatically in Android

I am trying to enable/disable mobile data.

First, is it possible to implement this?

No, that's not possible to achieve by third-party apps. Enabling/disabling mobile data should be done through system Settings app UI.

What you can do is to provide an instruction to end user on how to enable/disable mobile data and direct them to Settings app.



Related Topics



Leave a reply



Submit