我在尝试运行以下代码启动ClassNotFoundException
时得到BroadCastReceiver
。有一个custom notification
和一个buttonview
,当我点击按钮通知将关闭,但它给出以下例外。
我也在notification channel
方法中为API 26
和以上创建了OnCreate
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]]
从<receiver>
文件中删除了AndroidManifest.xml
标签,并将BroadcastReceiver
和IntentFilter
attributes
添加到BroadcastReceiver class
从Manifest文件中删除了以下标记
<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();
}
}