android 通知

  1. notification需要一個NotificationManager來管理,如何獲取呢?
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  2. 然后使用builder構造器來構造一個Notification對象,為了兼容性,最好使用v4包中的NotificationCompat
 Notification notification =  new NotificationCompat.Builder(this)
                .setContentTitle("hello")
                .setContentText("test")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon()
                .setLargeIcon()
                .build();
  1. 最后調用NotificationManager的notify()方法
  2. 以上步驟創(chuàng)建的通知是無法點擊的,需要設置PendingIntent
    PendingIntent有幾個靜態(tài)方法用來獲取對象
PendingIntent.getActivities()
PendingIntent.getBroadcast()
PendingIntent.getService()

NotificationCompat.Builder里有個方法:setContentIntent()來設置這個延遲的意圖

5.通知的關閉

  • 方式一:NotificationCompat.Builder中再加上setAutoCancel(),當通知被點擊后,消失
  • 方式二:顯式的調用NotificationManager的cancel()方法
  1. 更高級的通知,待續(xù)。。。
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容