Android消息通知欄的實現(xiàn)方法介紹
更新時間:2013年06月19日 11:34:25 作者:
本篇文章是對Android消息通知欄的實現(xiàn)方法進行了詳細的分析介紹,需要的朋友參考下
背景知識:可以用Activity和Service來開始消息通知,兩者的區(qū)別在于一個是在前臺觸發(fā),一個是后臺服務(wù)觸發(fā)。
要使用消息通知,必須要用到兩個類: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 = "我的通知欄標題";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
//定義下拉通知欄時要展現(xiàn)的內(nèi)容信息
Context context = getApplicationContext();
CharSequence contentTitle = "我的通知欄標展開標題";
CharSequence contentText = "我的通知欄展開詳細內(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方法通知用戶生成標題欄消息通知
mNotificationManager.notify(1, notification);
要使用消息通知,必須要用到兩個類: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 = "我的通知欄標題";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
//定義下拉通知欄時要展現(xiàn)的內(nèi)容信息
Context context = getApplicationContext();
CharSequence contentTitle = "我的通知欄標展開標題";
CharSequence contentText = "我的通知欄展開詳細內(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方法通知用戶生成標題欄消息通知
mNotificationManager.notify(1, notification);
您可能感興趣的文章:
- Android中通過Notification&NotificationManager實現(xiàn)消息通知
- Android編程實現(xiàn)google消息通知功能示例
- Android之開發(fā)消息通知欄
- Android自定義Notification添加點擊事件
- Android中AlarmManager+Notification實現(xiàn)定時通知提醒功能
- Android 中Notification彈出通知實現(xiàn)代碼
- Android編程使用Service實現(xiàn)Notification定時發(fā)送功能示例
- Android 通知使用權(quán)(NotificationListenerService)的使用
- android使用NotificationListenerService監(jiān)聽通知欄消息
- Android消息通知Notification常用方法(發(fā)送消息和接收消息)
相關(guān)文章
Android中新引進的Google Authenticator驗證系統(tǒng)工作原理淺析
這篇文章主要介紹了Android中新引進的Google Authenticator驗證系統(tǒng)工作原理淺析,需要的朋友可以參考下2014-10-10
Android 6.0上sdcard和U盤路徑獲取和區(qū)分方法
今天小編就為大家分享一篇Android 6.0上sdcard和U盤路徑獲取和區(qū)分方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
Android攔截并獲取WebView內(nèi)部POST請求參數(shù)的實現(xiàn)方法
這篇文章主要介紹了Android攔截并獲取WebView內(nèi)部POST請求參數(shù) 的實現(xiàn)方法,本文通過兩種方案給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04
Android使用phonegap從相冊里面獲取照片(代碼分享)
本文主要介紹了使用phonegap從相冊里面獲取照片的實現(xiàn)方法代碼。具有很好的參考價值,下面跟著小編一起來看下吧2017-03-03
Android系列---JSON數(shù)據(jù)解析的實例
JSON(JavaScript Object Notation)和XML,并稱為客戶端和服務(wù)端交互解決方案的倚天劍和屠龍刀,這篇文章主要介紹了Android系列---JSON數(shù)據(jù)解析的實例,有興趣的可以了解一下。2016-11-11
Android實現(xiàn)漸變啟動頁和帶有指示器的引導(dǎo)頁
這篇文章主要為大家詳細介紹了Android實現(xiàn)漸變啟動頁和帶有指示器的引導(dǎo)頁,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-09-09
Android自定義scrollview實現(xiàn)回彈效果
這篇文章主要為大家詳細介紹了Android自定義scrollview實現(xiàn)回彈效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04

