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

