Search code examples
androidxamarin.androidbroadcastreceiverandroid-broadcastreceiver

BroadcastReceiver ClassNotFoundException Xamarin Android


I am getting ClassNotFoundException when trying to run the below code to start BroadCastReceiver. there is a custom notification and a buttonview to it, when i click the button the notification will close, but it is giving the below exception.

and i have created the notification channel for API 26 and above in OnCreate method too

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:

<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>

Exception:

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]]

Solution

  • Removed <receiver> tag from AndroidManifest.xml file and added the BroadcastReceiver and IntentFilter attributes to the BroadcastReceiver class

    Removed the below tag from Manifest file

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

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