Forum Cross Platform with Xamarin

I am a complete noob to xamarin.How to force my app to use only cellular data ?

I have made an app that seemed perfect till I realized it crashes using cellular data but NOT using wi fi .Before any thing else I want to know how to tell my app to only use cellular data .Any answer or clue would be greatly appreciated.

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    For android, you can use following code to ForceWifiOverCellular or ForceCellularOverWifi

    using Android.Content;
    using Android.Net;
    using Android.OS;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace SumOfEntrys.Droid
    {
        class ForceNetworkType
        {
            public static Context _context = Android.App.Application.Context;
    
            /// <summary>
            /// Forces the wifi over cellular
            /// </summary>
            public static void ForceWifiOverCellular()
            {
                ConnectivityManager connection_manager = (ConnectivityManager)_context.GetSystemService(Context.ConnectivityService);
    
                NetworkRequest.Builder request = new NetworkRequest.Builder();
                request.AddTransportType(TransportType.Wifi);
    
                var callback = new ConnectivityManager.NetworkCallback();
                connection_manager.RegisterNetworkCallback(request.Build(), new CustomNetworkAvailableCallBack());
            }
    
            /// <summary>
            /// Forces the cellular over wifi.
            /// </summary>
            public static void ForceCellularOverWifi()
            {
                ConnectivityManager connection_manager = (ConnectivityManager)_context.GetSystemService(Context.ConnectivityService);
    
                NetworkRequest.Builder request = new NetworkRequest.Builder();
                request.AddTransportType(TransportType.Cellular);
    
                connection_manager.RegisterNetworkCallback(request.Build(), new CustomNetworkAvailableCallBack());
            }
        }
    
        /// <summary>
        /// Custom network available call back.
        /// </summary>
        public class CustomNetworkAvailableCallBack : ConnectivityManager.NetworkCallback
        {
            public static Context _context = Android.App.Application.Context;
    
            ConnectivityManager connection_manager = (ConnectivityManager)_context.GetSystemService(Context.ConnectivityService);
    
            public override void OnAvailable(Network network)
            {
                //ConnectivityManager.SetProcessDefaultNetwork(network);    //deprecated (but works even in Android P)
                connection_manager.BindProcessToNetwork(network);           //this works in Android P
            }
        }
    
    
    }
    

    For xamarin.form, you can create a dependence service to achieve it.

      public interface IChangeNetworkSettings
        {
            void ForceWifiOverCellular();
    
            void ForceCellularOverWifi();
        }
    

    In the android platform, you can achieve the interface.

    [assembly: Dependency(typeof(ChangeNetworkSettingsService))]
    namespace SumOfEntrys.Droid
    {
        public class ChangeNetworkSettingsService : IChangeNetworkSettings
        {
            public void ForceCellularOverWifi()
            {
                //throw new NotImplementedException();
                ForceNetworkType.ForceCellularOverWifi();
            }
    
            public void ForceWifiOverCellular()
            {
                // throw new NotImplementedException();
    
    
                ForceNetworkType.ForceWifiOverCellular();
            }
        }
    }
    

    But for iOS. it cannot be achieved, your app is sandboxed and can not access the Wifi manager's keychain, I cannot find a way to access it.


    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

Sign In or Register to comment.