phommy
phommy

Reputation: 55

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.

        var conn = ApplicationContext.GetSystemService(ConnectivityService).JavaCast<ConnectivityManager>();
        var connState = conn.StopUsingNetworkFeature(ConnectivityType.Mobile, "");

so my question is that: is there anyway to disable Mobile Data on Android with xamarin? or is there anyway to use "java reflection technique". with xamarin?

Upvotes: 2

Views: 2135

Answers (2)

Adit Kothari
Adit Kothari

Reputation: 421

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;

Upvotes: 0

matthewrdev
matthewrdev

Reputation: 12190

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.

Upvotes: 2

Related Questions