android Notification
是顯示在手機狀態欄上的通知,具備全局效果的通知
時效性不強
步驟:
// 得到系統服務
NotificationManager mNotificationManager
= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
// 建立一個Notification對象
Notification notification = new Notification(R.drawable.ic_launcher,
"短信來了", System.currentTimeMillis());
// 設置點擊事件關聯activity
Intent intent = new Intent(this, SecondActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, 0);
// 設置標題跟內容
notification.setLatestEventInfo(this, "小麗", "在麼", contentIntent);
// 發送通知
mNotificationManager.notify(0, notification);
// 點擊以後,消失
// notification.flags = Notification.FLAG_AUTO_CANCEL;
// 點擊以後,不會消失
notification.flags = Notification.FLAG_ONGOING_EVENT;
取消通知:
mNotificationManager.cancel(0);//參數必須是以前發送通知的idandroid