Check internet Connection

Vuyiswa Maseko 351 Reputation points
2022-04-19T12:31:07.907+00:00

Good Day All

i want to check if the user is connected to the internet , i switch off my wifi internet as depicted below for testing purpose

194341-no-inernet.png

and after that i tried to access a function that needs database connection , before i make a call , i call this function

public static bool CheckInternetConnection()  
{  
    var current = Connectivity.NetworkAccess;  
  
    if (current == NetworkAccess.Internet)  
    {  
        return true;  
    }  
    else  
    {  
        return false;  
    }  
}  

for some reason i am getting "True" instead of False to there is no internet on the device.

Thanks

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,312 questions
0 comments No comments
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 27,826 Reputation points Microsoft Vendor
    2022-04-20T03:31:26.293+00:00

    Hello,

    If you only switch off the internet on your Windows machine, the Android Emulator can't test the state. Please try to debug on your real device. Or open the airplane mode in Setting under NetWork&Internet of your Android Emulator.

    But VS can't debug in airplane mode on my side, because it is unable to connect debugger-agent. I'm not sure if this issue exists on your side.
    My testing on Android Emulator:

    public App()  
            {  
                InitializeComponent();  
                var current = Connectivity.NetworkAccess;  
                if (current == NetworkAccess.Internet)  
                {  
                    MainPage = new MainPage();  
                }  
                else  
                {  
                    MainPage = new NoInternetPage();  
                }        
            }  
    

    When I turn on airplane mode and reopen the app, it will go to NoInternetPage.

    Best Regards,
    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful