NotificationManager是負責通知用戶事件的發生,有三個方法:
1: cancel(id)取消廣播,假如這是一個暫時的廣播,則將其隱藏,如果是個持久的,則將其從中去除掉。
2:cancelAll():取消以前顯示的所有通知
3:notify(int id,Notification notification)把通知持久的發送到狀態條中。
下面直接把代碼貼上,代碼中有詳細解釋
private void addNotification() {
//獲取通知服務
NotificationManager manager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE) ;
Notification notifi = new Notification() ;
notifi.icon = R.drawable.ic_launcher ;//通知時候顯示的圖片
notifi.tickerText = "這是通知" ;
notifi.defaults = Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE ;//系統默認通知的聲音
//andioStreamType的值必須AudioManager的值,代表着響鈴模式
notifi.audioStreamType = android.media.AudioManager.ADJUST_LOWER ;
long[] vibreate= new long[]{1000,1000}; //震動的參數,震動一秒暫停一秒
notifi.vibrate = vibreate ;
Intent in = new Intent(MainActivity.this,SecondActivity.class) ;//點擊之後所跳轉到的activity
PendingIntent pend = PendingIntent.getActivity(this, 0, in, PendingIntent.FLAG_ONE_SHOT) ;
notifi.setLatestEventInfo(this, "這是標題嗎", "這是內容", pend);
manager.notify(0, notifi);//執行通知,這個0是用來區分這個廣播的唯一標誌,下面一句話中的0就是例證
//manager.cancel(0);//取消通知
}