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

时间:2011-07-20 16:46:49

标签: java android notifications broadcastreceiver statusbar

我正在尝试从广播接收器开始状态栏通知,然后从另一个广播接收器停止它,但我遇到了问题。我想在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);
    }
}

4 个答案:

答案 0 :(得分:9)

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

答案 1 :(得分:3)

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

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

希望这有助于任何同样困惑的人!

答案 2 :(得分:0)

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

答案 3 :(得分:0)

这是使用broadcastReceivers的通知的方法:

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

从BroadcastReceivers安排警报:

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

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