清除 Android 状态通知
我无法清除状态栏通知。
public void NotificationStatus(){
Intent intent = new Intent(this, Stimulation.class);
NotificationManager notificationManager
= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(
david.flanagan.android.VenAssist.Activities.R.drawable.notification_icon,
"VenAssist Stimulation ON", System.currentTimeMillis());
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.setLatestEventInfo(this, "VenAssist",
"Stimulation Started", activity);
notificationManager.notify(0, notification);
}
然后,我尝试通过调用“
notification.cancel(0);
我已创建通知实例”来清除服务的 onDestroy()
函数中的通知状态,但我不确定该实例是否正确。
private NotificationManager notification;
当我尝试取消服务并清除状态栏时,应用程序崩溃了。
Im having difficulty clearing a status bar Notification.
public void NotificationStatus(){
Intent intent = new Intent(this, Stimulation.class);
NotificationManager notificationManager
= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(
david.flanagan.android.VenAssist.Activities.R.drawable.notification_icon,
"VenAssist Stimulation ON", System.currentTimeMillis());
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.setLatestEventInfo(this, "VenAssist",
"Stimulation Started", activity);
notificationManager.notify(0, notification);
}
I'm then trying to clear the notification status in the onDestroy()
function of my service by calling
notification.cancel(0);
I have created an instance of the notification, which im not sure is correct.
private NotificationManager notification;
When I try to cancel the service, hence clear the status bar, the app crashes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的通知对象可能为 null,这将导致
NullPointerException
。我认为您应该使用
NotificationManager
对象来取消通知,使用相同的 id(在您的情况下为 0)并将其传递给NotificationManager.cancel(Int)
:NotificationManager 文档
Your notification object may be null, which would cause a
NullPointerException
.I think you should be using a
NotificationManager
object to cancel a notification, use the same id (in your case, 0) and pass that toNoticationManager.cancel(Int)
:NotificationManager docs