Introduction
Xamarin.Forms code runs on multiple platforms, each of which has its own filesystem. This means that reading and writing files is most easily done using the native file APIs on each platform. Alternatively, embedded resources are a simpler solution to distribute data files with an app.
Prerequisites
- Visual Studio 2017 or later (Windows or Mac)
Setting up a Xamarin.Forms Project
Start by creating a new Xamarin.Forms project. You wíll learn more by going through the steps yourself.
Create a new or existing Xamarin forms (.Net standard) Project. with Android and iOS Platforms.
Create a Interface
- public interface INetwork
- {
- bool IsConnected();
- bool IsConnectedFast();
- }
Create a NetworkConnectivity
The following code will check the Mobile network connection and check the network status.
NetworkConnectivity.cs
Android Implementation
NetworkHelper.cs
Here, implement the Interface and return the network status.
- [assembly: Xamarin.Forms.Dependency(typeof(NetworkHelper))]
- namespace NetworkPOC.Droid
- {
- public class NetworkHelper : INetwork
- {
- Context context = Android.App.Application.Context;
- public bool IsConnected()
- {
- return NetworkConnectivity.IsConnected(context);
- }
-
- public bool IsConnectedFast()
- {
- return NetworkConnectivity.IsConnectedFast(context);
- }
- }
- }
Consume the NetworkHelper
Here, you will call the Network helper class and you will get the network speed.
MainPage.Xaml
- <?xml version="1.0" encoding="utf-8" ?>
- <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
- xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
- xmlns:d="http://xamarin.com/schemas/2014/forms/design"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d"
- x:Class="NetworkPOC.MainPage">
-
- <StackLayout HorizontalOptions="Center" VerticalOptions="Start" Margin="0,150,0,0">
- <Label FontSize="Large" Text="Xamarin Monkeys"/>
- <Button Text="Check" Clicked="Button_Clicked"></Button>
- </StackLayout>
-
- </ContentPage>
Here I show the result in Toast.
MainPage.xaml.cs
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Xamarin.Forms;
- using Xamarin.Essentials;
- using System.Net.Http;
- namespace NetworkPOC
- {
-
-
- [DesignTimeVisible(false)]
- public partial class MainPage : ContentPage
- {
- public MainPage()
- {
- InitializeComponent();
- }
-
- var current = Connectivity.NetworkAccess;
-
- if (current == NetworkAccess.Internet)
- {
-
- bool isConnectionFast = DependencyService.Get<INetwork>().IsConnectedFast();
- if (isConnectionFast)
- DependencyService.Get<IToast>().ShowToast("Network Connection is good");
- else
- DependencyService.Get<IToast>().ShowToast("Network Connection is slow");
-
- }
- else
- {
- DependencyService.Get<IToast>().ShowToast("No Internet Connection");
- }
- }
- }
Run
I hope you have understood how to check your mobile network speed (slow or fast) using Android native in Xamarin.Forms.
Thanks for reading. Please share your comments and feedback.
Happy Coding :)