Starting and stopping a notification from a broadcast receiver

I am trying to start a status bar notification from a broadcast receiver and then stop it from another broadcast receiver, but I am having problems. I would like to start a notification in the status bar when usb is connected, and then when usb is disconnected, I would like to stop it. I have two receivers installed and it works, just struggling with starting and stopping one of the receiver, here is the code I have

My only mistake with my code is the line myNotificationManager = (NotificationManager) getSystemService(context.NOTIFICATION_SERVICE); , in which the error only says that getSystemService is undefined, and he wants to make a method which, I believe, means that the recipient does not have support for this method, such as activity, so what should I do to create and stop the notification from recipients, thanks for any help.

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

And then the receiver, when it turns it off, I think is good and should work. I think my problem is only in the USBConnect class

 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); } } 
+7
source share
4 answers

Well, I ended up finding out that for a future link for people with my same problem, all I had to do was add a context before getSystemService, here is my code that worked

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

And then the receiver, when it turns it off, I think is fine and should work. I think my problem is only in the USBConnect class

 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); } } 
+9
source

I myself ran into this problem. I think the user forgot to update the code in the response. context should be TWICE! I misunderstood it for the first time!

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

Hope this helps someone embarrass!

+3
source

getSystemService undefined most likely means that you have not imported android.app.Activity (getSystemService is defined in android.app.Activity).

0
source

This is the way to use notifications from broadcastReceivers:

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

Alarm Schedule from BroadcastReceivers:

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

You should always use a context prefix!

0
source

All Articles