清除状态通知的Andr​​oid [英] Clear a Status Notification Android

查看:181
本文介绍了清除状态通知的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林有困难的清除状态栏上的通知。

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);     
}

我试图再通过调用来清除我的服务的的onDestroy()函数通知状态

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.

推荐答案

您通知对象可能为null,这将导致 NullPointerException异常

Your notification object may be null, which would cause a NullPointerException.

我觉得你应该使用 NotificationManager 对象取消的通知,使用相同的ID(在你的情况,0),并传递到 NoticationManager.cancel(INT)

I think you should be using a NotificationManager object to cancel a notification, use the same id (in your case, 0) and pass that to NoticationManager.cancel(Int):

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(0);

NotificationManager文档

这篇关于清除状态通知的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆