- notification需要一個NotificationManager來管理,如何獲取呢?
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); - 然后使用builder構造器來構造一個Notification對象,為了兼容性,最好使用v4包中的NotificationCompat
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("hello")
.setContentText("test")
.setWhen(System.currentTimeMillis())
.setSmallIcon()
.setLargeIcon()
.build();
- 最后調用NotificationManager的notify()方法
- 以上步驟創(chuàng)建的通知是無法點擊的,需要設置PendingIntent
PendingIntent有幾個靜態(tài)方法用來獲取對象
PendingIntent.getActivities()
PendingIntent.getBroadcast()
PendingIntent.getService()
NotificationCompat.Builder里有個方法:setContentIntent()來設置這個延遲的意圖
5.通知的關閉
- 方式一:NotificationCompat.Builder中再加上setAutoCancel(),當通知被點擊后,消失
- 方式二:顯式的調用NotificationManager的cancel()方法
- 更高級的通知,待續(xù)。。。