好吧,我最终自己弄清楚了,以供将来遇到同样问题的人参考,我所要做的就是在getSystemService前面添加上下文,这是我的代码有效
public class USBConnect extends BroadcastReceiver {public NotificationManager myNotificationManager;public static final int NOTIFICATION_ID = 1;@Overridepublic void onReceive(Context context, Intent intent) { myNotificationManager = (NotificationManager) getSystemService(context.NOTIFICATION_SERVICE); CharSequence NotificationTicket = "USB Connected!"; CharSequence NotificationTitle = "USB Connected!"; CharSequence NotificationContent = "USB is Connected!"; Notification notification = new Notification(R.drawable.usbicon, NotificationTicket, 0); Intent notificationIntent = new Intent(context, MyClass.class); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); notification.setLatestEventInfo(context, NotificationTitle, NotificationContent, contentIntent); notification.flags |= Notification.FLAG_ONGOING_EVENT; myNotificationManager.notify(NOTIFICATION_ID, notification); }}然后接收器断开连接时,我认为这很好,应该可以工作,我认为我的问题仅在USBConnect类中
public class USBDisCon extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) { NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.cancel(USBConnect.NOTIFICATION_ID); }}


