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,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 the Permission for Network and Location services.

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
 <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 :-

using System; 
 using Android.App; 
 using Android.Content; 
 using Android.Runtime; 
 using Android.Views; 
 using Android.Widget; 
 using Android.OS; 
 using Android.Locations; 
 namespace CheckNetwork 
 { 
 [Activity(Label = "CheckNetwork", MainLauncher = true, Icon = "@drawable/icon")] 
 public class MainActivity: Activity 
 { 
 protected override void OnCreate(Bundle bundle) 
 { 
 base.OnCreate(bundle); 
 // Set our view from the "main" layout resource 
 SetContentView(Resource.Layout.Main); 
 // Get our button from the layout resource, 
 // and attach an event to it 
 Button button = FindViewById < Button > (Resource.Id.MyButton); 
 button.Click += delegate 
 { 
 Android.Net.ConnectivityManager connectivityManager = (Android.Net.ConnectivityManager) GetSystemService(ConnectivityService); 
 Android.Net.NetworkInfo activeConnection = connectivityManager.ActiveNetworkInfo; 
 bool isOnline = (activeConnection != null) && activeConnection.IsConnected; 
 if (isOnline == false) 
 { 
 Toast.MakeText(this, "Network Not Enable", ToastLength.Long).Show(); 
 } else 
 { 
 LocationManager mlocManager = (LocationManager) GetSystemService(LocationService);; 
 bool enabled = mlocManager.IsProviderEnabled(LocationManager.GpsProvider); 
 if (enabled == false) 
 { 
 Toast.MakeText(this, "GPS Not Enable", ToastLength.Long).Show(); 
 } 
 } 
 }; 
 } 
 } 
 }

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.

 

Download Source here

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

Leave a Comment

Your email address will not be published. Required fields are marked *