BroadcastReceiver ClassNotFoundException Xamarin Android

BroadcastReceiver ClassNotFoundException Xamarin Android

我在尝试 运行 下面的代码启动 BroadCastReceiver 时收到 ClassNotFoundException。有一个 custom notification 和一个 buttonview,当我单击按钮时,通知将关闭,但它给出以下异常。

并且我也在 OnCreate 方法中为 API 26 及更高版本创建了 notification channel

public void OpenActivity(object sender, EventArgs e)
    {
         Intent intent = new Intent(this, typeof(DrawRect));
         intent.SetFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);

         PendingIntent pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

         RemoteViews remoteView = new RemoteViews(PackageName, Resource.Layout.NotificationLayout);

         int notificationId = (int)SystemClock.CurrentThreadTimeMillis();
         Intent buttonIntent = new Intent("button_clicked");
         buttonIntent.PutExtra("id", notificationId);

         PendingIntent buttonPendingIntent = 
         PendingIntent.GetBroadcast(this, notificationId, buttonIntent, 0);

         remoteView.SetOnClickPendingIntent(Resource.Id.cloceNotification, 
         buttonPendingIntent);

         NotificationCompat.Builder notify = new 
         NotificationCompat.Builder(this, "Diet")
                        .SetSmallIcon(Resource.Mipmap.icon)
                        .SetContentTitle("Diet")
                        .SetContentText("My App")
                    .SetPriority(NotificationCompat.PriorityHigh).SetContentIntent(pendingIntent).SetOngoing(true).SetStyle(new NotificationCompat.DecoratedCustomViewStyle()).SetCustomContentView(remoteView);


         NotificationManagerCompat notificationCompat = NotificationManagerCompat.From(this);
         notify.Build().Flags |= NotificationFlags.OnlyAlertOnce;
         notificationCompat.Notify(notificationId, notify.Build());
         FinishAndRemoveTask();
     }

BroadCastReceiver Class:

public class Button_listener : BroadcastReceiver
    {
         public override void OnReceive(Context context, Intent intent)
         {
              NotificationManagerCompat notificationManager = NotificationManagerCompat.From(context);
              notificationManager.Cancel(intent.GetIntExtra("id",1));
              Toast.MakeText(context, "From Broadcaster", ToastLength.Long).Show();
          }
    }

清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="DietCam.DietCam" android:installLocation="auto">
      <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="28" />
      <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
      <uses-permission android:name="android.permission.INTERNET" />

      <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"  android:debuggable="true">
         <activity android:label="DrawRect" android:theme="@style/Theme.AppCompat.Transparent.NoActionBar" android:name="md580a1eddd40074c89f21a5ec99d8b044c.DrawRect"  android:screenOrientation="sensor"/>
          <activity android:label="SplashScreen" android:name="md580a1eddd40074c89f21a5ec99d8b044c.SplashScreen">

          <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
          </intent-filter>
        </activity>   

        <receiver android:name=".Main.Button_listener">
                <intent-filter>
                    <action android:name="button_clicked"/>
                </intent-filter>
         </receiver>
      </application>
    </manifest>

异常:

Unhandled Exception:

    Java.Lang.RuntimeException: Unable to instantiate receiver DietCam.DietCam.Main.Button_listener: java.lang.ClassNotFoundException: Didn't find class "DietCam.DietCam.Main.Button_listener" on path: DexPathList[[zip file "/data/app/DietCam.DietCam-1/base.apk"],nativeLibraryDirectories=[/data/app/DietCam.DietCam-1/lib/x86, /data/app/DietCam.DietCam-1/base.apk!/lib/x86, /system/lib, /vendor/lib]]

AndroidManifest.xml 文件中删除了 <receiver> 标签并将 BroadcastReceiverIntentFilter attributes 添加到 BroadcastReceiver class

从清单文件中删除了以下标签

<receiver android:name=".Main.Button_listener">
      <intent-filter>
          <action android:name="button_clicked"/>
      </intent-filter>
</receiver>

为 BroadcastReceiver 添加了属性 class:

[BroadcastReceiver(Enabled = true, Exported = false)]
[IntentFilter(new[] { "button_clicked" } )]
public class Button_listener : BroadcastReceiver
{
     public override void OnReceive(Context context, Intent intent)
     {
          NotificationManagerCompat notificationManager = NotificationManagerCompat.From(context);

          notificationManager.Cancel(intent.GetIntExtra("id",1));
          Toast.MakeText(context, "From Broadcaster", ToastLength.Long).Show();
     }
}