启动和停止来自广播接收器的通知 [英] Starting and stopping a notification from broadcast receiver

查看:22
本文介绍了启动和停止来自广播接收器的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从广播接收器启动状态栏通知,然后从另一个广播接收器停止它,但我遇到了问题.我想在 USB 连接时在状态栏中启动通知,然后当 USB 断开连接时我想停止它我设置了两个接收器并且正在努力从接收器启动和停止一个这里是代码我目前有

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

我的代码唯一的错误是 myNotificationManager = (NotificationManager) getSystemService(context.NOTIFICATION_SERVICE); 行错误只是说 getSystemService 未定义,它想要制作我猜这意味着接收器不像活动那样支持该方法,所以我应该怎么做才能创建和停止来自接收器的通知,谢谢您的帮助

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

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

推荐答案

好吧,我最终自己弄清楚了,以供将来有同样问题的人参考,我需要做的就是在此处的 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);
    }
}

这篇关于启动和停止来自广播接收器的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆