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
- <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);
-
- SetContentView(Resource.Layout.Main);
-
-
- 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 Notification. Here, 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.