Reputation: 298
I have to check internet connection in my application, fallowing this answer
I have added lines to my manifest file. However I have an issue with getSystemServiceMethod
it is not defined in my case, context does not contain such a method.
As well I`ve been trying to do something like this:
public static bool isInternetOn()
{
ConnectivityManager connectivityManager = (ConnectivityManager)Context.ConnectivityService;
NetworkInfo networkInfo = connectivityManager.ActiveNetworkInfo;
bool isWifi = networkInfo.Type == ConnectivityType.Wifi; // wifi check
return networkInfo.IsConnected;
}
But it throws an exception. How could I fix this issue and check whether the phone has access to the internet.
Upvotes: 0
Views: 2323
Reputation: 7850
Context
as parameter of isInternetOn
.or
connectivityManager = (ConnectivityManager)(Application.Context.GetSystemService(Context.ConnectivityService));
or
Your code have a bug, you must check networkInfo
nullity.
It should look like this:
public static bool isInternetOn()
{
var connectivityManager = (ConnectivityManager)(Application.Context.GetSystemService(Context.ConnectivityService));
NetworkInfo networkInfo = connectivityManager.ActiveNetworkInfo;
return networkInfo != null && networkInfo.IsConnected;
}
FINAL NOTE:
Your implementation don't cover all the cases, please see. You should use ConnectivityPlugin.
Upvotes: 4