Android消息通知欄的實(shí)現(xiàn)方法介紹
更新時(shí)間:2013年06月19日 11:34:25 作者:
本篇文章是對(duì)Android消息通知欄的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
背景知識(shí):可以用Activity和Service來開始消息通知,兩者的區(qū)別在于一個(gè)是在前臺(tái)觸發(fā),一個(gè)是后臺(tái)服務(wù)觸發(fā)。
要使用消息通知,必須要用到兩個(gè)類:NotificationManager和Notification,其他NotificationManager的初始化是用getSystemService方法,并且通過notify方法來向android系統(tǒng)發(fā)送消息欄通知和顯示。
效果 :

代碼:
//消息通知欄
//定義NotificationManager
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
//定義通知欄展現(xiàn)的內(nèi)容信息
int icon = R.drawable.icon;
CharSequence tickerText = "我的通知欄標(biāo)題";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
//定義下拉通知欄時(shí)要展現(xiàn)的內(nèi)容信息
Context context = getApplicationContext();
CharSequence contentTitle = "我的通知欄標(biāo)展開標(biāo)題";
CharSequence contentText = "我的通知欄展開詳細(xì)內(nèi)容";
Intent notificationIntent = new Intent(this, BootStartDemo.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText,
contentIntent);
//用mNotificationManager的notify方法通知用戶生成標(biāo)題欄消息通知
mNotificationManager.notify(1, notification);
要使用消息通知,必須要用到兩個(gè)類:NotificationManager和Notification,其他NotificationManager的初始化是用getSystemService方法,并且通過notify方法來向android系統(tǒng)發(fā)送消息欄通知和顯示。
效果 :

代碼:
復(fù)制代碼 代碼如下:
//消息通知欄
//定義NotificationManager
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
//定義通知欄展現(xiàn)的內(nèi)容信息
int icon = R.drawable.icon;
CharSequence tickerText = "我的通知欄標(biāo)題";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
//定義下拉通知欄時(shí)要展現(xiàn)的內(nèi)容信息
Context context = getApplicationContext();
CharSequence contentTitle = "我的通知欄標(biāo)展開標(biāo)題";
CharSequence contentText = "我的通知欄展開詳細(xì)內(nèi)容";
Intent notificationIntent = new Intent(this, BootStartDemo.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText,
contentIntent);
//用mNotificationManager的notify方法通知用戶生成標(biāo)題欄消息通知
mNotificationManager.notify(1, notification);
您可能感興趣的文章:
- Android中通過Notification&NotificationManager實(shí)現(xiàn)消息通知
- Android編程實(shí)現(xiàn)google消息通知功能示例
- Android之開發(fā)消息通知欄
- Android自定義Notification添加點(diǎn)擊事件
- Android中AlarmManager+Notification實(shí)現(xiàn)定時(shí)通知提醒功能
- Android 中Notification彈出通知實(shí)現(xiàn)代碼
- Android編程使用Service實(shí)現(xiàn)Notification定時(shí)發(fā)送功能示例
- Android 通知使用權(quán)(NotificationListenerService)的使用
- android使用NotificationListenerService監(jiān)聽通知欄消息
- Android消息通知Notification常用方法(發(fā)送消息和接收消息)
相關(guān)文章
Android實(shí)現(xiàn)View滑動(dòng)效果的6種方法
這篇文章主要介紹了Android實(shí)現(xiàn)View滑動(dòng)的6種方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03
Android中新引進(jìn)的Google Authenticator驗(yàn)證系統(tǒng)工作原理淺析
這篇文章主要介紹了Android中新引進(jìn)的Google Authenticator驗(yàn)證系統(tǒng)工作原理淺析,需要的朋友可以參考下2014-10-10
Android 6.0上sdcard和U盤路徑獲取和區(qū)分方法
今天小編就為大家分享一篇Android 6.0上sdcard和U盤路徑獲取和區(qū)分方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08
Android攔截并獲取WebView內(nèi)部POST請求參數(shù)的實(shí)現(xiàn)方法
這篇文章主要介紹了Android攔截并獲取WebView內(nèi)部POST請求參數(shù) 的實(shí)現(xiàn)方法,本文通過兩種方案給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04
Android使用phonegap從相冊里面獲取照片(代碼分享)
本文主要介紹了使用phonegap從相冊里面獲取照片的實(shí)現(xiàn)方法代碼。具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-03-03
Android系列---JSON數(shù)據(jù)解析的實(shí)例
JSON(JavaScript Object Notation)和XML,并稱為客戶端和服務(wù)端交互解決方案的倚天劍和屠龍刀,這篇文章主要介紹了Android系列---JSON數(shù)據(jù)解析的實(shí)例,有興趣的可以了解一下。2016-11-11
Android實(shí)現(xiàn)漸變啟動(dòng)頁和帶有指示器的引導(dǎo)頁
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)漸變啟動(dòng)頁和帶有指示器的引導(dǎo)頁,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09
Android自定義scrollview實(shí)現(xiàn)回彈效果
這篇文章主要為大家詳細(xì)介紹了Android自定義scrollview實(shí)現(xiàn)回彈效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04

