我试图从广播接收器启动状态栏通知,然后停止来自另一个广播接收器,但我遇到了问题。我想开始状态栏中的通知,当USB连接,然后当USB断开连接,我想停止它我有两个接收器设置和工作只是努力开始和停止一个接收器这里是代码我目前开始和停止来自广播接收器的通知
我唯一的错误与我的代码是myNotificationManager = (NotificationManager) getSystemService(context.NOTIFICATION_SERVICE);
行错误只是说getSystemService是未定义的,它想要做的方法,我猜的意思是接收器不支持该方法就像一个活动会所以我应该怎么做才能创造和停止接收通知感谢您的帮助
public class USBConnect extends BroadcastReceiver {
public NotificationManager myNotificationManager;
public static final int NOTIFICATION_ID = 1;
@Override
public 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);
}
}
然后是REC eiver为当断开这个我相信是好的,应该工作,我想我的问题是只有在USBCONNECT类
public class USBDisCon extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(USBConnect.NOTIFICATION_ID);
}
}
我有那实际导入,我仍然得到它是未定义的,它想创建方法 – user577732
没有人有任何想法?真的很感谢一些帮助 – user577732