Xamarin Android: Create Android Application To Check Network And GPS Status

Let’s start,

Step 1 - Open Visual Studio, New Project, Templates, Visual C#, Android, then select Blank App (Android)

Select Blank App, then give Project Name and Project Location.



Step 2

Next go to Solution Explorer, Project Name, Properties, then AndroidManifest.xml to open the Code page. Then give permission for Network and Location services.

XML Code

  1. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
  2.   
  3. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  
  4.   
  5. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  

Step 3 - Next to open Solution Explorer, Project Name, then MainActivity.cs open and write the following C# Code.

C# Code

  1. using System;  
  2. using Android.App;  
  3. using Android.Content;  
  4. using Android.Runtime;  
  5. using Android.Views;  
  6. using Android.Widget;  
  7. using Android.OS;  
  8. using Android.Locations;  
  9. namespace CheckNetwork  
  10. {  
  11.     [Activity(Label = "CheckNetwork", MainLauncher = true, Icon = "@drawable/icon")]  
  12.     public class MainActivity: Activity  
  13.     {  
  14.         protected override void OnCreate(Bundle bundle)   
  15.         {  
  16.             base.OnCreate(bundle);  
  17.             // Set our view from the "main" layout resource  
  18.             SetContentView(Resource.Layout.Main);  
  19.             // Get our button from the layout resource,  
  20.             // and attach an event to it  
  21.             Button button = FindViewById < Button > (Resource.Id.MyButton);  
  22.             button.Click += delegate  
  23.             {  
  24.                 Android.Net.ConnectivityManager connectivityManager = (Android.Net.ConnectivityManager) GetSystemService(ConnectivityService);  
  25.                 Android.Net.NetworkInfo activeConnection = connectivityManager.ActiveNetworkInfo;  
  26.                 bool isOnline = (activeConnection != null) && activeConnection.IsConnected;  
  27.                 if (isOnline == false)  
  28.                 {  
  29.                     Toast.MakeText(this"Network Not Enable", ToastLength.Long).Show();  
  30.                 } else   
  31.                 {  
  32.                     LocationManager mlocManager = (LocationManager) GetSystemService(LocationService);;  
  33.                     bool enabled = mlocManager.IsProviderEnabled(LocationManager.GpsProvider);  
  34.                     if (enabled == false)  
  35.                     {  
  36.                         Toast.MakeText(this"GPS Not Enable", ToastLength.Long).Show();  
  37.                     }  
  38.                 }  
  39.             };  
  40.         }  
  41.     }  
  42. }  

Step 4 - Press F5 or Build and Run the Application,Click Button once checking the GPS and Network Status, then show output NotificationHere, I disabled only GPS, so the output will come GPS Status only.

 

Finally, we have successfully created Xamarin Application for checking Network and GPS Status.

Up Next
    Ebook Download
    View all
    Learn
    View all