private static final int NOTIFICATION_ID=250;  //用来标示notification,通过notificatinomanager来发布同样标示的notification将更新旧的通知,同时取消notification也需要这个标示(android很多地方都用到标示这么一个玩意)
  
private void setNotification(String note) {
     //获取系统通知服务
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification();
notification.icon = R.drawable.notify; //通知图标,官方文档中说必须有icon才能显示出notification
notification.when = System.currentTimeMillis();
notification.tickerText = note; //通知出现在标题栏时显示的内容
notification.flags = Notification.FLAG_ONGOING_EVENT; // 设置常驻Flag
notification.defaults = Notification.DEFAULT_SOUND; //设置notification的提示音为系统默认提示声音
notification.setLatestEventInfo(this, "提示消息", note, null);
notificationManager.notify(NOTIFICATION_ID, notification);
} // 取消通知
private void cancelNotification() {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
}
04-26 15:44