Java 启动和停止来自广播接收器的通知

Java 启动和停止来自广播接收器的通知,java,android,notifications,broadcastreceiver,statusbar,Java,Android,Notifications,Broadcastreceiver,Statusbar,我试图从一个广播接收器启动状态栏通知,然后从另一个广播接收器停止,但我遇到问题。我想在usb连接时启动状态栏中的通知,然后在usb断开连接时我想停止它我已经设置了两个接收器,正在努力从一个接收器启动和停止其中一个。这是我目前的代码 我的代码中唯一的错误是myNotificationManager=(NotificationManager)getSystemService(context.NOTIFICATION\u服务)行错误只是说getSystemService未定义,它想创建一个方法,我猜这

我试图从一个广播接收器启动状态栏通知,然后从另一个广播接收器停止,但我遇到问题。我想在usb连接时启动状态栏中的通知,然后在usb断开连接时我想停止它我已经设置了两个接收器,正在努力从一个接收器启动和停止其中一个。这是我目前的代码

我的代码中唯一的错误是
myNotificationManager=(NotificationManager)getSystemService(context.NOTIFICATION\u服务)行错误只是说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);
      }
}
然后是接收器,当它断开连接时,我认为这是好的,应该可以工作。我认为我的问题只存在于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);
    }
}
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);
    }
}

getSystemService未定义很可能意味着您尚未导入android.app.Activity(getSystemService在android.app.Activity中定义).

好吧,最后我自己弄明白了,以备将来有同样问题的人参考。我需要做的就是在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);
      }
}
然后是接收器,当它断开连接时,我认为这是好的,应该可以工作。我认为我的问题只存在于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);
    }
}
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);
    }
}

我自己也遇到了这个问题。我想用户忘记更新答案中的代码了。
上下文
需要输入两次!我第一次读错了

myNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

希望这能帮助那些同样困惑的人

这是使用广播接收者通知的方式:

NotificationManager mgr = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
安排来自广播接收器的警报:

NotificationManager mgr = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

您必须始终使用上下文前缀

我已经导入了它,但我仍然知道它是未定义的,它想要创建一种方法。没有人有任何想法吗?非常感谢你的帮助