How To Disable Mobile Data On Android With Xamarin
similar question to How to disable Mobile Data on Android . the only difference is that i want to do that with Xamarin but not java. I tried the code below but it did nothing.
Solution 1:
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:
voidSetMobileDataEnabled(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.ConnectivityManagerconman= (Android.Net.ConnectivityManager)GetSystemService (ConnectivityService);
Java.Lang.ClassconmanClass= Java.Lang.Class.ForName (conman.Class.Name);
Java.Lang.Reflect.FieldiConnectivityManagerField= conmanClass.GetDeclaredField ("mService");
iConnectivityManagerField.Accessible = true;
Java.Lang.ObjectiConnectivityManager= iConnectivityManagerField.Get (conman);
Java.Lang.ClassiConnectivityManagerClass= Java.Lang.Class.ForName (iConnectivityManager.Class.Name);
Java.Lang.Reflect.MethodsetMobileDataEnabledMethod= iConnectivityManagerClass.GetDeclaredMethod ("setMobileDataEnabled", Java.Lang.Boolean.Type);
setMobileDataEnabledMethod.Accessible = true;
setMobileDataEnabledMethod.Invoke (iConnectivityManager, enabled);
}
if (Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.Gingerbread) {
TelephonyManagertm= (TelephonyManager)GetSystemService (Context.TelephonyService);
Java.Lang.ClasstelephonyClass= Java.Lang.Class.ForName (tm.Class.Name);
Java.Lang.Reflect.MethodgetITelephonyMethod= telephonyClass.GetDeclaredMethod ("getITelephony");
getITelephonyMethod.Accessible = true;
Java.Lang.Objectstub= getITelephonyMethod.Invoke (tm);
Java.Lang.ClassITelephonyClass= Java.Lang.Class.ForName (stub.Class.Name);
Java.Lang.Reflect.MethoddataConnSwitchMethod=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.
Solution 2:
In xamarin android you can use like this if you want to disable the mobile cellular data. TelephonyManager tm = (TelephonyManager)Android.App.Application.Context.GetSystemService(Context.TelephonyService);
var tdata = tm.DataEnabled;
if (tdata)
tdata = false;
Post a Comment for "How To Disable Mobile Data On Android With Xamarin"