【问题标题】:Starting and stopping a notification from broadcast receiver启动和停止来自广播接收器的通知
【发布时间】:2011-10-09 13:51:19
【问题描述】:

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

我的代码唯一的错误是myNotificationManager = (NotificationManager) getSystemService(context.NOTIFICATION_SERVICE); 行,错误只是说 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);
    }
}

【问题讨论】:

    标签: java android notifications broadcastreceiver statusbar


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我自己也遇到过这个问题。我认为用户忘记更新答案中的代码。 context需要参加两次!我第一次看错了!

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

      希望这对同样困惑的人有所帮助!

      【讨论】:

        【解决方案3】:

        getSystemService undefined 很可能是你没有导入android.app.Activity(getSystemService在android.app.Activity中定义)。

        【讨论】:

        • 我实际上已经导入了它,但我仍然知道它是未定义的,它想要创建方法
        • 没人有什么想法吗?非常感谢一些帮助
        【解决方案4】:

        这是使用来自广播接收器的通知的方式:

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

        从 BroadcastReceivers 安排警报:

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

        您必须始终使用上下文前缀!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-12-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多