use*_*732 8 java notifications android statusbar broadcastreceiver
我正在尝试从广播接收器开始状态栏通知,然后从另一个广播接收器停止它,但我遇到了问题.我想在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);
}
}
Run Code Online (Sandbox Code Playgroud)
然后接收器,当它断开这个我相信是好的,应该工作我认为我的问题只在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);
}
}
Run Code Online (Sandbox Code Playgroud)
use*_*732 10
好吧,我最终将自己搞清楚,以便将来参考同样问题的人我需要做的就是在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);
}
}
Run Code Online (Sandbox Code Playgroud)
然后接收器为什么它断开这我相信是好的,应该工作我认为我的问题只在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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8585 次 |
| 最近记录: |