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