Forum Xamarin.Android

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Verify Connection internet

I'm wanting to check connection NET, but when I lose the connection using this code my test does not work well, even removing the network cable the connection status is true. (Test in Emulator Android)

Code:

        ConnectivityManager conMannager = (ConnectivityManager)GetSystemService(Context.ConnectivityService);

        if (conMannager.GetNetworkInfo(ConnectivityType.Mobile).GetState() == NetworkInfo.State.Connected ||

            conMannager.GetNetworkInfo(ConnectivityType.Mobile).GetState() == NetworkInfo.State.Connecting)
        {
            RunOnUiThread(() =>
               {
                   statusWifi.Text = "CONECTADO";
               });
        }

        if (conMannager.GetNetworkInfo(0).GetState() == NetworkInfo.State.Disconnected ||

            conMannager.GetNetworkInfo(0).GetState() == NetworkInfo.State.Disconnecting)
        {
            RunOnUiThread(() =>
            {
                statusWifi.Text = "DESCONECTADO";
            });
        }

Posts

  • TomOpgenorthTomOpgenorth CAXamarin Team Xamurai

    Hmmm, my guess is this that this is an emulator issue (your code looks like it should work). There is probably an emulator setting that you can tweak to disable the internet connection. Maybe go into the settings for the emulator and turn on Airplane Mode - that should disconnect the internet.

  • EdgarKLEdgarKL USMember

    On My opinion this code will not test the real Internet Rechability. It is testing network interfaces. If You are disconnecting the cable your emulator still connected locally with Your computer. If You want to surely know whether you are connected, you should make a request to resource that is 100% available (for example Google).

Sign In or Register to comment.