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

// 添加常驻通知
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() 取消该通知。

posted on 2013-04-08 23:50  屌丝迷途  阅读(1835)  评论(0编辑  收藏  举报

导航