简体   繁体   English

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

[英]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我想在 usb 连接时在状态栏中启动通知,然后当 usb 断开连接时我想停止它我已经设置了两个接收器,并且正在努力从接收器启动和停止一个接收器这是代码我目前有

My only error with my code is the myNotificationManager = (NotificationManager) getSystemService(context.NOTIFICATION_SERVICE);我的代码唯一错误是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行错误只是说 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);
      }
}

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然后接收器断开连接时我相信它很好并且应该可以工作我认为我的问题只在 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好吧,我最终自己弄清楚了,以供有同样问题的人将来参考我需要做的就是在 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);
      }
}

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然后接收器断开连接时我认为这很好并且应该可以工作我认为我的问题仅在 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);
    }
}

I encountered this problem myself.我自己也遇到了这个问题。 I think the user forgot to update the code in the answer.我认为用户忘记更新答案中的代码。 context needs to be in TWICE! context需要在 TWICE 中! I misread it the first time!我第一次看错了!

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

Hope this helps anyone similarly confused!希望这可以帮助任何同样困惑的人!

This is the way to use notification from broadcastReceivers:这是使用来自广播接收器的通知的方式:

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

Schedule alarms from BroadcastReceivers:从 BroadcastReceivers 安排警报:

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

You have to use the context prefix always!您必须始终使用上下文前缀!

getSystemService undefined most likely means you have not imported android.app.Activity (getSystemService is defined in android.app.Activity). getSystemService undefined 很可能意味着您没有导入 android.app.Activity(getSystemService 在 android.app.Activity 中定义)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM