其中第2个和第3个notification使用的是同一个id(r.drawabgle.why),因此,第3个notification会覆盖第2个notification。
在显示notification时还可以设置显示通 知时的默认发声、震动和light效果。要实现这个功能需要设置notification类的defaults属性,代码如下:
java代码:
复制到剪贴板 java代码
notification.defaults = notification.default_sound; // 使用默认的声音
notification.defaults = notification.default_vibrate; // 使用默认的震动
notification.defaults = notification.default_lights; // 使用默认的light
notification.defaults = notification.default_all; // 所有的都使用默认值
注意:设置默认发声、震动和light的方法是setdefaults。该 方法与shownotification方法的实现代码基本相同,只是在调用notify方法之前需要设置defaults属性(defaults属性必须在 调用notify方法之前调用,否则不起作用)。在设置默认震动效果时还需要在androidmanifest.xml文件中通过<uses-permission>标签设置android.permission.vibrate权限。
如果要清除某个消息,可以使用notificationmanager类 的cancel方 法,该方法只有一个参数,表示要清除的notification的id。使用cancelall可以清除当前notificationmanager对象中的所有notification。
运行本节的例子,单击屏幕上显示notification的按钮,会显示如 图1所示的消息。每一个消息会显示一会,然后就只显示整个android系统(也包括其他应用程序) 的notification(只显示图像部分)。如图2所示。如果将状态栏拖下来,可以看 到notification的详细信息和发出通知的时间(也就是notification类的构造方法的第3个参数值),如图3所
示。当单击【清除通知】按钮,会清除本应用程序显示的所有notification,清除后的效果如图4所示。