Question

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 ?

Was it helpful?

Solution

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?

OTHER TIPS

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 ();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top