I am developing Xamarin.Android app. In my app , I want to implement a background service , which will run indefinitely to detect changes in network status. i.e. if network connection goes off , it should send such notification . It should send notification too when network connection becomes on(device got a network connection). Can anyone tell me how to implement such service in Xamarin.Android ?
Asked
Active
Viewed 8,145 times
2 Answers
3
Create object of following class by passing Activity
context. Your application network handled this class.
public class NetworkReachability
{
Activity activity;
Thread checkNetworkActiveThread;
String conectedVia;
ConnectivityManager connectivityManager;
bool isDialogShowing;
AlertDialog.Builder builder;
public NetworkReachability (Activity activity)
{
this.activity = activity;
connectivityManager = (ConnectivityManager)activity.GetSystemService (Context.ConnectivityService);
this.doAlertDialog ();
this.CheckNetworkRechability ();
}
private void CheckNetworkRechability ()
{
checkNetworkActiveThread = new Thread (new ThreadStart (CheckNetworkAvailable));
checkNetworkActiveThread.Start ();
}
private async void CheckNetworkAvailable ()
{
bool isNetwork = await Task.Run (() => this.NetworkRechableOrNot ());
if (!isNetwork) {
activity.RunOnUiThread (() => {
try {
if (!isDialogShowing) {
isDialogShowing = true;
builder.Show ();
}
} catch (Exception ex) {
Console.WriteLine ("NetworkReachability -> CheckNetworkRechability:" + ex.Message);
}
});
} else {
isDialogShowing = false;
this.CheckNetworkAvailable ();
}
}
private bool NetworkRechableOrNot ()
{
var activeConnection = connectivityManager.ActiveNetworkInfo;
if ((activeConnection != null) && activeConnection.IsConnected) {
// we are connected to a network.
if (activeConnection.Type.ToString ().ToUpper ().Equals ("WIFI")) {
return true;
} else
return true;
} else {
return false;
}
}
private void doAlertDialog ()
{
if (builder != null)
builder.Dispose ();
builder = new AlertDialog.Builder (activity);
builder.SetTitle ("Network not reachable");
builder.SetMessage ("Plz check network");
builder.SetCancelable (true);
builder.SetNegativeButton ("Retry", AlertRetryClick);
builder.SetPositiveButton ("Cancel", AlertCancelClick);
}
private void AlertRetryClick (object sender1, DialogClickEventArgs args)
{
isDialogShowing = false;
this.CheckNetworkAvailable ();
}
private void AlertCancelClick (object sender1, DialogClickEventArgs args)
{
isDialogShowing = false;
Intent intent = new Intent(activity.Application.ApplicationContext, typeof(SplashActivity));
intent.AddFlags ( ActivityFlags.ClearTop | ActivityFlags.NewTask | ActivityFlags.ClearTask);
intent.PutExtra (Constant.EXIT, Constant.EXIT);
activity.StartActivity(intent);
activity.Finish ();
}
}

Orkhan Alikhanov
- 9,122
- 3
- 39
- 60

Tushar Thakar
- 322
- 2
- 8
2
How much reasearch did you do?
Have a look on the ConnectivityManager: http://developer.xamarin.com/recipes/android/networking/networkinfo/detect_network_connection/
You could make a listener yourself, which fires an event when it changes?
Network listener Android and How can I monitor the network connection status in Android?

Community
- 1
- 1

Casper Skoubo
- 310
- 2
- 9
-
Thank you @Casper Skoubo , your links are worked for me.Thanks a lot !! – Suraj Jul 30 '14 at 10:29
-
@Suraj is this working in closed app too? Or only suppended app? – Alexsandro Aug 10 '16 at 21:54