How To Check NetworkInfo In Xamarin Android App Using Visual Studio 2015

In this article, you will learn how to Check NetworkInfo in Xamarin Android app, using Visual Studio 2015.

Introduction

Xamarin is a platform to develop cross-platform and multi-platform apps (for example, Windows phone, Android, iOS).

In Xamarin platform, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available.

NetworkInfo is called Check Network and is enabled or disabled.

Prerequisites

  • Visual Studio 2015 Update 3.

The steps, mentioned below are required to be followed in order to check NetworkInfo in Xamarin Android app, using Visual Studio 2015.

Step 1

Click File--> select New--> select Project. The project needs to be clicked after opening all the types of projects in Visual Studio
or click (Ctrl+Shift+N).

phone

Step 2

After opening the New Project, select Installed-->Templates-->Visual C#-->Android-->choose the Blank app (Android).

Now, give your Android app a name (Ex:sample) and give the path of your project. Afterwards, click OK.

project

Step 3

Now, go to Solution Explorer. In Solution Explorer, get all the files and sources in your project.

Now, Select Resource-->Layout-->double click to open main.axml page. To write XAML code, you need to select the source.

Choose the Designer Window, if you want design it, and you can design your app.

project

Step 4

After opening main.axml, file will open the main page designer. You can design the page, as per your desire..

project

Now, delete Default hello world button. Go to the source panel and you can see the button coding. You need to delete it.

After deleting XAML code, delete C# button action code.

Go to the MainActivity.cs page. You need to delete the button code.

Step 5

Now, go to the toolbox Window. In the toolbox Window, get all the types of the tools and controls.

You need to go to the toolbox Window. Now, scroll down and you will see all the tools and controls.

You need to drag and drop the Button.

project

Step 6

Now, go to the properties Window. You need to edit the Button's Id value and Text value (EX: android:id="@+id/MyButton" android:text="Check Network").

project

Step 7

In this step, go to the Main.axml page Source Panel. Note, the Button's Id value.

Main.axml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:minWidth="25px" android:minHeight="25px">  
  2.     <Button android:text="Check Network" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/MyButton" /> </LinearLayout>  
project

Step 8

In this step, go to the MainActivity.cs page in Solution Explorer. Add the namespace, mentioned below.

MainActivity.cs

Using Android.Locations;

project

Step 9

Now, write the code, mentioned below in MainActivity.cs page.

MainActivity.cs
  1. namespace networkcheck {  
  2.     [Activity(Label = "networkcheck", MainLauncher = true, Icon = "@drawable/icon")]  
  3.     public class MainActivity: Activity {  
  4.         protected override void OnCreate(Bundle bundle) {  
  5.             base.OnCreate(bundle);  
  6.             // Set our view from the "main" layout resource  
  7.             SetContentView(Resource.Layout.Main);  
  8.             Button button = FindViewById < Button > (Resource.Id.MyButton);  
  9.             button.Click += delegate {  
  10.                 Android.Net.ConnectivityManager connectivityManager = (Android.Net.ConnectivityManager) GetSystemService(ConnectivityService);  
  11.                 Android.Net.NetworkInfo activeConnection = connectivityManager.ActiveNetworkInfo;  
  12.                 bool isOnline = (activeConnection != null) && activeConnection.IsConnected;  
  13.                 if (isOnline == false) {  
  14.                     Toast.MakeText(this"Network is Disable", ToastLength.Long).Show();  
  15.                 } else {  
  16.                     LocationManager mlocManager = (LocationManager) GetSystemService(LocationService);;  
  17.                     bool enabled = mlocManager.IsProviderEnabled(LocationManager.GpsProvider);  
  18.                     if (enabled == false) {  
  19.                         Toast.MakeText(this"GPS is Disable", ToastLength.Long).Show();  
  20.                     }  
  21.                 }  
  22.             };  
  23.         }  
  24.     }  
  25. }  
project

Step 10

In this step, give the required permissions in your app.

Go to the Solution Explorer--> properties-->Right click-->Open.

project

Step 11

After opening the properties options, select Android Manifest-->Required Permissions-->Check Permissions, mentioned below.

ACCESS_FINE_LOCATION
ACCESS_NETWORK_STATE
ACCESS_WIFI_STATE

project

Step 12

If you have an Android Virtual device, run the app on it, else connect your Android phone and run the app on it.

Simply, connect your phone and go to Visual Studio. The connected phone will show up in the Run menu (Ex:LENOVO A6020a40 (Android 5.1-API 22)). Click Run option.

project

Output

After a few seconds, the app will start running on your phone.

You will see your app is running successfully. Now, you will click the Check Network Button.

project

Now, you will see NetworkInfo is successful.

project

Summary

Thus, this was the process of how to check NetworkInfo in Xamarin Android app, using Visual Studio 2015.