xamarin.BroadcastReceiver ClassNotFoundException Xamarin And



我在尝试运行以下代码开始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类:

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类中添加了属性:

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

最新更新