开始和停止广播接收器的通知

我试图从广播接收器开始状态栏通知,然后停止从另一个广播接收器,但我有问题。 我想在状态栏中启动通知,当USB连接,然后当USB连接断开,我想停止它我有两个接收机设置和工作只是努力开始和停止从接收器这里是代码我目前有

我唯一的错误与我的代码是myNotificationManager = (NotificationManager) getSystemService(context.NOTIFICATION_SERVICE); 行的错误只是说getSystemService是未定义的,它想要做的方法,我猜意味着接收器不支持该方法就像一个活动会如何,我应该怎么做来创build和停止接收器的通知谢谢你任何帮助

 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); } } 

  • 在Android的每个星期一重复报警使用AlarmManager / BroadcastReceiver
  • ACTION_BATTERY_CHANGED像疯了一样开火
  • 为什么在BroadcastReceiver中调用setResult?
  • adb shell的BroadcastReceiver权限
  • dynamic注册vs BroadcastReceiver的静态注册
  • 广播接收器检测应用程序启动
  • queryBroadcastReceivers没有find向registerReceiver注册的接收者
  • 在Android中无法检测到何时应答拨出呼叫
  • 那么我最终搞清楚了自己为将来的参考为同样的问题的人我所需要做的就是在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); } } 

    我自己也遇到过这个问题。 我认为用户忘了更新答案中的代码。 context需要在两次! 我第一次误读

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

    希望这可以帮助任何人类似的困惑!

    getSystemService undefined最有可能意味着你没有导入android.app.Activity(getSystemService是在android.app.Activity中定义的)。

    这是使用broadcastReceivers通知的方式:

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

    从BroadcastReceivers安排警报:

     AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 

    你必须始终使用上下文前缀!