Android 通知的基本用法示例代碼
寫android通知的時候發(fā)現(xiàn)Notification的setLatestEventInfo被棄用,于是搜素并整理了一下新的android通知的基本用法。
一、獲取NotificationManager實例
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
二、創(chuàng)建Notification實例
在這里需要根據(jù)project的min-sdk來選擇實現(xiàn)方法,MIN API Level < 11的可以使用setLatestEventInfo()方法,以下介紹API Level 11 之后的Notification實例獲取方法。
1. MIN API Level < 16 構(gòu)建Notification實例的方法
1) 創(chuàng)建Notification.Builder實例
Notification.Builder builder = new Notification.Builder(context) .setAutoCancel(true) //設置點擊通知后自動取消通知 .setContentTitle("title") //通知標題 .setContentText("describe") //通知第二行的內(nèi)容 .setContentIntent(pendingIntent) //點擊通知后,發(fā)送指定的PendingIntent .setSmallIcon(R.drawable.ic_launcher); //通知圖標,必須設置否則通知不顯示
2) 調(diào)用Notification.Builder的getNotification()方法獲得Notification
notification = builder.getNotification();
2. MIN API Level >=16 構(gòu)建Notification實例的方法
Notification notification = new Notification.Builder(context) .setAutoCancel(true) .setContentTitle("title") .setContentText("text") .setSmallIcon(R.mipmap.ic_launcher) .setContentIntent(pendingIntent) .build();
三、發(fā)送通知
notificationManager.notify(1,notification);
以上就是對Android 通知欄的知識資料整理,后續(xù)繼續(xù)補充,謝謝大家對本站的支持。
- android實現(xiàn)通知欄下載更新app示例
- Android實現(xiàn)沉浸式通知欄通知欄背景顏色跟隨app導航欄背景顏色而改變
- Android消息通知欄的實現(xiàn)方法介紹
- Android中通知Notification使用實例(振動、燈光、聲音)
- android中創(chuàng)建通知欄Notification代碼實例
- Android開發(fā)之禁止下拉通知欄的方法
- Android實現(xiàn)通知欄透明的方法
- Android程序版本更新之通知欄更新下載安裝
- Android開發(fā)之使用通知欄顯示提醒信息的方法
- Android中通過Notification&NotificationManager實現(xiàn)消息通知
相關文章
2021最新Android筆試題總結(jié)美團Android崗職能要求
這篇文章主要介紹了2021最新Android筆試題總結(jié)以及美團Android崗職能要求,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08android 獲取本機的IP地址和mac物理地址的實現(xiàn)方法
本文主要介紹android 獲取本機的IP地址和mac物理地址的實現(xiàn)方法,這里提供示例代碼,實現(xiàn)功能,有需要的小伙伴可以參考下2016-09-09Android仿百度谷歌搜索自動提示框AutoCompleteTextView簡單應用示例
這篇文章主要介紹了Android仿百度谷歌搜索自動提示框AutoCompleteTextView簡單應用,結(jié)合實例形式分析了AutoCompleteTextView Widget使用步驟與相關操作技巧,需要的朋友可以參考下2016-10-10Android中TextureView與SurfaceView用法區(qū)別總結(jié)
TextureView和SurfaceView都是繼承自View類的,TextureView在Andriod4.0之后才引入的,SurfaceView不能加上動畫、平移、縮放,TextureView可以但有1-3幀的延遲2018-04-04