How to monitor internet connection in xamarin.android?

RajeshNegiRajeshNegi USMember ✭✭

Hey i want to implement this feature in my android application in which--
when the internet is off it will give Internet is off with refresh method in snackbar
other wise the application will run.
In the running application if internet is not there it should give internet if off refresh..
i have implemented this at application start but unable to do continuously......!!!!!!!!

Answers

  • VarunBabuSVarunBabuS INMember ✭✭✭

    hi @RajeshNegi for implementation of asset tracking where we capture geo-coordinates in background i used the following link for reference https://developer.xamarin.com/samples/mobile/BackgroundLocationDemo/ using the same concept may be at regular intervals you can may be using some components to detect internet connectivity and alert user appropriately

  • RajeshNegiRajeshNegi USMember ✭✭

    I want to implement Broadcast Receiver ....
    which takes the information from android os

  • cwphillicwphilli USMember ✭✭✭

    Below is a solution that provides the ability to subscribe to events, i.e. ConnectivityChanged.

    Personally I added this via nuget in my packages.config:
    <package id="Xam.Plugin.Connectivity" version="2.3.0" targetFramework="portable46-net451+win81" />

    https://www.nuget.org/packages/Xam.Plugin.Connectivity
    https://components.xamarin.com/view/connectivityplugin

  • cwphillicwphilli USMember ✭✭✭
        private static void CurrentOnConnectivityTypeChanged(object sender, ConnectivityTypeChangedEventArgs connectivityTypeChangedEventArgs)
        {
            if (!connectivityTypeChangedEventArgs.IsConnected) return;
            var availableConnections = string.Join(",", connectivityTypeChangedEventArgs.ConnectionTypes);
            Console.WriteLine($"Connections available: {availableConnections}");
            // TODO: store available connections?
        }
    
        private static void CurrentOnConnectivityChanged(object sender, ConnectivityChangedEventArgs connectivityChangedEventArgs)
        {
            if (connectivityChangedEventArgs.IsConnected)
            {
                Console.WriteLine($"Connectivity has been restored");
                // TODO: maintain connection status"
            }
        }
    
        private static void SetupConnectionListeners()
        {
            CrossConnectivity.Current.ConnectivityChanged += CurrentOnConnectivityChanged;
            CrossConnectivity.Current.ConnectivityTypeChanged += CurrentOnConnectivityTypeChanged;
        }
    
  • RajeshNegiRajeshNegi USMember ✭✭

    where i need to put this code..

Sign In or Register to comment.