Android消息通知欄的實(shí)現(xiàn)方法介紹
更新時(shí)間:2013年06月19日 11:34:25 作者:
本篇文章是對(duì)Android消息通知欄的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
背景知識(shí):可以用Activity和Service來(lái)開(kāi)始消息通知,兩者的區(qū)別在于一個(gè)是在前臺(tái)觸發(fā),一個(gè)是后臺(tái)服務(wù)觸發(fā)。
要使用消息通知,必須要用到兩個(gè)類:NotificationManager和Notification,其他NotificationManager的初始化是用getSystemService方法,并且通過(guò)notify方法來(lái)向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)展開(kāi)標(biāo)題";
CharSequence contentText = "我的通知欄展開(kāi)詳細(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方法,并且通過(guò)notify方法來(lái)向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)展開(kāi)標(biāo)題";
CharSequence contentText = "我的通知欄展開(kāi)詳細(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中通過(guò)Notification&NotificationManager實(shí)現(xiàn)消息通知
- Android編程實(shí)現(xiàn)google消息通知功能示例
- Android之開(kāi)發(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)聽(tīng)通知欄消息
- Android消息通知Notification常用方法(發(fā)送消息和接收消息)
相關(guān)文章
Android提高之AudioRecord實(shí)現(xiàn)助聽(tīng)器的方法
這篇文章主要介紹了Android中AudioRecord實(shí)現(xiàn)助聽(tīng)器的方法,對(duì)進(jìn)行Android項(xiàng)目開(kāi)發(fā)有一定的借鑒價(jià)值,需要的朋友可以參考下2014-08-08Android編程實(shí)現(xiàn)長(zhǎng)按彈出選項(xiàng)框View進(jìn)行操作的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)長(zhǎng)按彈出選項(xiàng)框View進(jìn)行操作的方法,結(jié)合實(shí)例形式分析了Android事件響應(yīng)及彈窗的功能、布局相關(guān)操作技巧,需要的朋友可以參考下2017-06-06Android權(quán)限操作之uses-permission詳解
這篇文章主要介紹了Android權(quán)限操作之uses-permission,較為詳細(xì)的分析了uses-permission常見(jiàn)權(quán)限操作類型與功能,需要的朋友可以參考下2016-10-10Android使用自定義ImageView實(shí)現(xiàn)圓形圖片效果
本篇文章主要介紹了Android使用自定義ImageView實(shí)現(xiàn)圓形圖片效果,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05Android開(kāi)發(fā)獲取當(dāng)前系統(tǒng)日期和時(shí)間功能示例
這篇文章主要介紹了Android開(kāi)發(fā)獲取當(dāng)前系統(tǒng)日期和時(shí)間功能,結(jié)合實(shí)例形式分析了Android布局、事件響應(yīng)、監(jiān)聽(tīng)以及時(shí)間獲取相關(guān)操作技巧,需要的朋友可以參考下2019-04-04Kotlin語(yǔ)言中CompileSdkVersion與targetSdkVersion的區(qū)別淺析
這篇文章主要介紹了Kotlin語(yǔ)言中CompileSdkVersion和targetSdkVersion有什么區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-02-02