如何在 C# Xamarin API 28 中使用带有回调的 PendingIntent 而不是带有 ConnectivityManager.ConnectivityAction 的 Intent?

How to use PendingIntent instead of an Intent with ConnectivityManager.ConnectivityAction with a callback in C# Xamarin API 28?

我需要在 Xamarin 中做一个简单的回调,来检查网络状态是连接还是断开。

到目前为止我一直在用这个代码来做:

class NetworkControl : INetworkControl
{
    private readonly INetworkControl.ICallback _callback;
    private readonly Context _context;
    private readonly NetworkBroadcastReceiver _receiver = new NetworkBroadcastReceiver();

    public NetworkControl(INetworkControl.ICallback callback, Context context)
    {
        _callback = callback;
        _context = context;
        IntentFilter filter = new IntentFilter(ConnectivityManager.ConnectivityAction);
        context.RegisterReceiver(_receiver, filter);
    }

    public INetworkControl.ICallback Callback => _callback;

    public INetworkControl.NetworkStatus Status
    {
        get
        {
            var current = Connectivity.NetworkAccess;
            if (current == NetworkAccess.Internet)
            {
                return INetworkControl.NetworkStatus.Connected;
            }
            return INetworkControl.NetworkStatus.Disconnected;
        }
    }
}


class NetworkBroadcastReceiver : BroadcastReceiver
{

    private static String TAG = "NetworkBroadcastReceiver";

    public override void OnReceive(Context context, Intent intent)
    {


       if (ShellBridge.Instance != null)
       {
           if (intent.Action.Equals(ConnectivityManager.ConnectivityAction))
           {
               NetworkInfo ni = (NetworkInfo)intent.Extras.Get(ConnectivityManager.ExtraNetworkInfo);
               if (ni.isConnected) 
               {
                   // do something if connected
                   ShellBridge.Instance.NetworkBridge.Callback.NetworkStatusChanged(INetworkControl.NetworkStatus.Connected);
               } else 
               {
                   ShellBridge.Instance.NetworkBridge.Callback.NetworkStatusChanged(INetworkControl.NetworkStatus.Connected);
               }

           }

       }

    }

问题是,Intent creating 中的函数 ConnectivityManager.ConnectivityAction 已经被弃用,很快就会过时。经过搜索,我发现 pendingIntent 应该用于此目的,但我找不到任何有效的示例来说明如何使用它。

最接近我需要的是:


但是,它没有我需要的所有信息。 我需要它全部以编程方式进行,而不更改清单,因为我的应用程序应该是一个前台和后台应用程序。

请帮忙,谢谢你的时间。

您不需要重新发明轮子。您可以使用 Xamarin Essentials 的 Connectivity.

实现所有这些

除了检查是否有这样的连接:

var current = Connectivity.NetworkAccess;
if (current == NetworkAccess.Internet)
{
    // Connection to internet is available
}

您还可以跟踪连接类型何时发生变化:

public class ConnectivityTest
{
    public ConnectivityTest()
    {
        // Register for connectivity changes, be sure to unsubscribe when finished
        Connectivity.ConnectivityChanged += Connectivity_ConnectivityChanged;
    }

    void Connectivity_ConnectivityChanged(object sender, ConnectivityChangedEventArgs e)
    {
        var access = e.NetworkAccess;
        var profiles = e.ConnectionProfiles;
    }
}

你可以看看NetworkCallback

public class ConnectionStateMonitor : NetworkCallback  
    {
        NetworkRequest networkRequest;

        public ConnectionStateMonitor()
        {
            networkRequest = new NetworkRequest.Builder().
                AddTransportType(TransportType.Cellular).
                AddTransportType(TransportType.Wifi).Build();
        }

        public void enable(Context context) {
            ConnectivityManager connectivityManager = context.GetSystemService(Context.ConnectivityService) as ConnectivityManager;
            connectivityManager.RegisterNetworkCallback(networkRequest, this);
        }

        public override void OnAvailable(Network network)
        {
            //network available
        }

        public override void OnLost(Network network)
        {
            //network lost
        }
    }

用法

你只需要实例化 class ConnectionStateMonitor 并启用它,你可以使用 OnAvailableOnLost 方法检测网络状态。

ConnectionStateMonitor m = new ConnectionStateMonitor ();
m.enable(context);

参考

https://github.com/xamarin/Essentials/issues/512