栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

好吧,我最终自己弄清楚了,以供将来遇到同样问题的人参考,我所要做的就是在getSystemService前面添加上下文,这是我的代码有效

public class USBConnect extends BroadcastReceiver {public NotificationManager myNotificationManager;public static final int NOTIFICATION_ID = 1;@Overridepublic 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 {@Overridepublic void onReceive(Context context, Intent intent) {    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);     notificationManager.cancel(USBConnect.NOTIFICATION_ID);    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/438618.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号