Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting and stopping a notification from broadcast receiver

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);
    }
}
like image 888
user577732 Avatar asked Jul 20 '11 16:07

user577732


People also ask

How do you stop broadcasting on Android?

CB stands for Cell Broadcast. To stop recieving CB messages, go to Messaging then tap the Menu key and select Settings. New menu appears then please, find CB activation and Uncheck it.

How to stop foreground notification Android?

In your implementation, use the same foreground id you used for your main foreground service. The parameter (true) in the stopForeground method call indicates that the notification should be removed. Now, we just have to start this dummy service in the service we want to run in the foreground without notification.

How notification and broadcast is used in Android studio?

A broadcast receiver is a dormant component of the Android system. Only an Intent (for which it is registered) can bring it into action. The Broadcast Receiver's job is to pass a notification to the user, in case a specific event occurs. Using a Broadcast Receiver, applications can register for a particular event.


1 Answers

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);
    }
}
like image 78
user577732 Avatar answered Oct 21 '22 11:10

user577732