用于检测Xamarin.Android中的网络状态的后台服务

时间:2014-07-30 07:07:16

标签: xamarin xamarin.android xamarin-studio

我正在开发Xamarin.Android应用程序。 在我的应用程序中,我想实现一个后台服务,它将无限期地运行以检测网络状态的变化。 即如果网络连接断开,它应该发送这样的通知。 当网络连接开启时(设备有网络连接),它也应该发送通知。 谁能告诉我如何在Xamarin.Android中实现这样的服务?

2 个答案:

答案 0 :(得分:3)

通过传递Activity上下文来创建以下类的对象。您的应用程序网络处理了此类。

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 ();
    }
}

答案 1 :(得分:2)

你做了多少研究?

查看ConnectivityManager: http://developer.xamarin.com/recipes/android/networking/networkinfo/detect_network_connection/

你可以自己创建一个监听器,它会在事件发生变化时触发它吗?

Network listener AndroidHow can I monitor the network connection status in Android?