Android中在通知栏内常驻应用程序消息

来源:互联网 发布:程序员最讨厌几阿哥 编辑:程序博客网 时间:2024/06/05 05:44
http://leybreeze.com/blog/?p=1480


// 添加常驻通知private void setNotification() {    NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);    Notification notification = new Notification(R.drawable.icon, getString(R.string.app_name), System.currentTimeMillis());    Intent intent = new Intent(this, Sample.class);    notification.flags = Notification.FLAG_ONGOING_EVENT; // 设置常驻 Flag    PendingIntent contextIntent = PendingIntent.getActivity(this, 0, intent, 0);    notification.setLatestEventInfo(getApplicationContext(), getString(R.string.app_name), getString(R.string.information), contextIntent);    notificationManager.notify(R.string.app_name, notification);} // 取消通知private void cancelNotification() {    NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);    notificationManager.cancel(R.string.app_name);}

说明:通过 setNotification() 可以在通知栏中添加一条常驻通知,通过 cancelNotification() 取消该通知。