OnReceive OnReceive 触发两次

OnReceive OnReceive firing twice

我为 Android 编写了这个 Xamarin 代码,以便在设备的网络连接发生变化时收到通知。为什么当我启用或禁用 wifi 或飞行模式时 OnReceive 触发两次?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Net;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace BroadcastReceiverTest
{
  [BroadcastReceiver(Enabled = true, Exported = true)]
  [IntentFilter(new string[] { ConnectivityManager.ConnectivityAction })]
  public class MyBroadcastReceiver : BroadcastReceiver
  {
    private const string TAG = "MyBroadcastReceiver";

    public override void OnReceive(Context context, Intent intent)
    {
      ConnectivityManager connectivityManager = (ConnectivityManager)context.GetSystemService(Context.ConnectivityService);
      NetworkInfo activeNetInfo = connectivityManager.ActiveNetworkInfo;
      if (activeNetInfo != null)
      {
        Android.Util.Log.Debug(TAG, "Receiver : " + activeNetInfo);
      }
      else
      {
        Android.Util.Log.Debug(TAG, "Receiver : " + "No network");
      }
    }
  }
}

我不知道我所说的是否在 Xamarin 的文档中,但发生这种情况是因为您可能正在使用 class 属性 BroadcastReceiver.

在我的测试中,将此属性放在 class 中不再需要通过 RegisterReceiver 方法或应用程序的 Android 清单文件注册接收器 class。

尝试以下示例,使用应用程序模板空白 Xamarin Android:

BatrianoReceiver.cs

//Comment this line if you register the Receiver class with RegisterReceiver method or in the Android Manifest file
[BroadcastReceiver(Label = "Batriano custom Receiver")]
[IntentFilter(new string[] { "com.batressc.batriano.CUSTOM_RECEIVER" })]
public class BatrianoReceiver : BroadcastReceiver {
    public override void OnReceive(Context context, Intent intent) {
        string extraData = intent.GetStringExtra("BatrianoData");
        if (extraData != null) Log.Info("batriano", extraData);
        else Log.Warn("batriano", "No se encontraron datos");
    }
}

MainActivity.cs

[Activity(Label = "Uso Receivers", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity {
    private BatrianoReceiver receiver;

    protected override void OnCreate(Bundle bundle) {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.Main);
        Button button = FindViewById<Button>(Resource.Id.MyButton);
        button.Click += (sender, e) => {
            Intent batriano = new Intent();
            batriano.SetAction("com.batressc.batriano.CUSTOM_RECEIVER");
            batriano.PutExtra("BatrianoData", string.Format("{0}-{1}", Guid.NewGuid(), DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss.fff tt")));
            SendBroadcast(batriano);
        };
        //Uncomment this line if you comment [BroadcastReceiver] attribute and register the receiver class with RegisterReceiver method of in Android Manifest file
        //Register();
    }

    protected override void OnStop() {
        base.OnStop();
        //Uncomment this line if you comment [BroadcastReceiver] attribute and register the receiver class with RegisterReceiver method of in Android Manifest file
        //Unregister();
    }

    protected override void OnResume() {
        base.OnResume();
        //Uncomment this line if you comment [BroadcastReceiver] attribute and register the receiver class with RegisterReceiver method of in Android Manifest file
        //Register();
    }

    private void Unregister() {
        if (receiver != null) {
            UnregisterReceiver(receiver);
            receiver = null;
        }
    }

    private void Register() {
        if (receiver == null) {
            receiver = new BatrianoReceiver();
            RegisterReceiver(receiver, new IntentFilter("com.batressc.batriano.CUSTOM_RECEIVER"));
        }
    }
}