I'm trying to start a status bar notification from a broadcast receiver and then stop it from another broadcast receiver but I'm having issues. I would like to start the notification in the status bar when usb is connected and then when usb is disconnected I would like to stop it I have the two receivers set up and working just struggling with starting and stopping one from a receiver here is the code I have currently
My only error with my code is the myNotificationManager = (NotificationManager) getSystemService(context.NOTIFICATION_SERVICE);
line the error just says getSystemService is undefined and it wants to make the method which I guess means that the receiver doesn't have support for that method like an activity would so what should I do to create and stop a notification from receivers thank you 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 for when it disconnects this I believe 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);
}
}
Well i ended up figuring it out myself for future reference for people with my same issue all i needed to do was add context in front of 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 for when it disconnects this i believe 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);
}
}