android通知欄的實(shí)現(xiàn)方法分析
本文實(shí)例講述了android通知欄的實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
這幾天一直在修改twigee的源代碼,其中一個(gè)要加入的功能是常駐Notification欄,以前寫的時(shí)候只能出現(xiàn) 在“通知”這一組中,想把它放在“正在運(yùn)行”組中卻不知道怎么放,查了下官方文檔,找到了方法,在notification的flags字段中加一下 “FLAG_ONGOING_EVENT”就可以了。同時(shí)我也把Notification的使用方法給總結(jié)了一下。詳見下文:
(1)、使用系統(tǒng)定義的Notification
以下是使用示例代碼:
//創(chuàng)建一個(gè)NotificationManager的引用 String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager)getSystemService(ns); // 定義Notification的各種屬性 int icon = R.drawable.icon; //通知圖標(biāo) CharSequence tickerText = "Hello"; //狀態(tài)欄顯示的通知文本提示 long when = System.currentTimeMillis(); //通知產(chǎn)生的時(shí)間,會(huì)在通知信息里顯示 //用上面的屬性初始化 Nofification Notification notification = new Notification(icon,tickerText,when); /* * 添加聲音 * notification.defaults |=Notification.DEFAULT_SOUND; * 或者使用以下幾種方式 * notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3"); * notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6"); * 如果想要讓聲音持續(xù)重復(fù)直到用戶對(duì)通知做出反應(yīng),則可以在notification的flags字段增加"FLAG_INSISTENT" * 如果notification的defaults字段包括了"DEFAULT_SOUND"屬性,則這個(gè)屬性將覆蓋sound字段中定義的聲音 */ /* * 添加振動(dòng) * notification.defaults |= Notification.DEFAULT_VIBRATE; * 或者可以定義自己的振動(dòng)模式: * long[] vibrate = {0,100,200,300}; //0毫秒后開始振動(dòng),振動(dòng)100毫秒后停止,再過200毫秒后再次振動(dòng)300毫秒 * notification.vibrate = vibrate; * long數(shù)組可以定義成想要的任何長度 * 如果notification的defaults字段包括了"DEFAULT_VIBRATE",則這個(gè)屬性將覆蓋vibrate字段中定義的振動(dòng) */ /* * 添加LED燈提醒 * notification.defaults |= Notification.DEFAULT_LIGHTS; * 或者可以自己的LED提醒模式: * notification.ledARGB = 0xff00ff00; * notification.ledOnMS = 300; //亮的時(shí)間 * notification.ledOffMS = 1000; //滅的時(shí)間 * notification.flags |= Notification.FLAG_SHOW_LIGHTS; */ /* * 更多的特征屬性 * notification.flags |= FLAG_AUTO_CANCEL; //在通知欄上點(diǎn)擊此通知后自動(dòng)清除此通知 * notification.flags |= FLAG_INSISTENT; //重復(fù)發(fā)出聲音,直到用戶響應(yīng)此通知 * notification.flags |= FLAG_ONGOING_EVENT; //將此通知放到通知欄的"Ongoing"即"正在運(yùn)行"組中 * notification.flags |= FLAG_NO_CLEAR; //表明在點(diǎn)擊了通知欄中的"清除通知"后,此通知不清除, * //經(jīng)常與FLAG_ONGOING_EVENT一起使用 * notification.number = 1; //number字段表示此通知代表的當(dāng)前事件數(shù)量,它將覆蓋在狀態(tài)欄圖標(biāo)的頂部 * //如果要使用此字段,必須從1開始 * notification.iconLevel = ; // */ //設(shè)置通知的事件消息 Context context = getApplicationContext(); //上下文 CharSequence contentTitle = "My Notification"; //通知欄標(biāo)題 CharSequence contentText = "Hello World!"; //通知欄內(nèi)容 Intent notificationIntent = new Intent(this,Main.class); //點(diǎn)擊該通知后要跳轉(zhuǎn)的Activity PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0); notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); //把Notification傳遞給 NotificationManager mNotificationManager.notify(0,notification);
如果想要更新一個(gè)通知,只需要在設(shè)置好notification之后,再次調(diào)用 setLatestEventInfo(),然后重新發(fā)送一次通知即可,即再次調(diào)用notify()。
(2)、使用自定義的 Notification
要 創(chuàng)建一個(gè)自定義的Notification,可以使用RemoteViews。要定義自己的擴(kuò)展消息,首先 要初始化一個(gè)RemoteViews對(duì)象,然后將它傳遞給Notification的contentView字段,再把PendingIntent傳遞給 contentIntent字段。以下示例代碼是完整步驟:
1、創(chuàng)建一個(gè)自 定義的消息布局 view.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_marginRight="10dp" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="fill_parent" android:textColor="#000" /> </LinearLayout>
2、 在程序代碼中使用RemoteViews的方法來定義image和text。然后把RemoteViews對(duì)象傳到contentView字段
RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.view); contentView.setImageViewResource(R.id.image,R.drawable.icon); contentView.setTextViewText(R.id.text,”Hello,this message is in a custom expanded view”); notification.contentView = contentView;
3、 為Notification的contentIntent字段定義一個(gè)Intent(注意,使用自定義View不需要 setLatestEventInfo()方法)
Intent notificationIntent = new Intent(this,Main.class); PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0); notification.contentIntent = contentIntent;
4、發(fā)送通知
mNotificationManager.notify(2,notification);
以下是全部示例代碼:
//創(chuàng)建一個(gè) NotificationManager的引用 String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager)getSystemService(ns); // 定義Notification的各種屬性 int icon = R.drawable.icon; //通知圖標(biāo) CharSequence tickerText = "Hello"; //狀態(tài)欄顯示的通知文本提示 long when = System.currentTimeMillis(); //通知產(chǎn)生的時(shí)間,會(huì)在通知信息里顯示 //用上面的屬性初始化 Nofification Notification notification = new Notification(icon,tickerText,when); RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.view); contentView.setImageViewResource(R.id.image, R.drawable.iconempty); contentView.setTextViewText(R.id.text, "Hello,this is JC"); notification.contentView = contentView; Intent notificationIntent = new Intent(this,Main.class); PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0); notification.contentIntent = contentIntent; //把Notification傳遞給NotificationManager mNotificationManager.notify(0,notification);
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- android實(shí)現(xiàn)通知欄下載更新app示例
- android中創(chuàng)建通知欄Notification代碼實(shí)例
- Android開發(fā)之禁止下拉通知欄的方法
- Android程序版本更新之通知欄更新下載安裝
- Android消息通知欄的實(shí)現(xiàn)方法介紹
- Android開發(fā)之使用通知欄顯示提醒信息的方法
- Android實(shí)現(xiàn)通知欄透明的方法
- Android編程獲取通知欄高度的方法
- Android獲取常用輔助方法(獲取屏幕高度、寬度、密度、通知欄高度、截圖)
- android項(xiàng)目實(shí)現(xiàn)帶進(jìn)度條的系統(tǒng)通知欄消息
- Android項(xiàng)目仿UC瀏覽器和360手機(jī)衛(wèi)士消息常駐欄(通知欄)
- android使用NotificationListenerService監(jiān)聽通知欄消息
- Android編程實(shí)現(xiàn)上方通知欄里閃動(dòng)效果的方法
相關(guān)文章
如何在原有Android項(xiàng)目中快速集成React Native詳解
創(chuàng)建一個(gè)React Native項(xiàng)目并寫一個(gè)純的 React Native 應(yīng)用可以參考官方指南。下面這篇文章主要給大家介紹了關(guān)于如何在原有Android項(xiàng)目中快速集成React Native的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-12-12Android實(shí)現(xiàn)自定義圓形進(jìn)度條
這篇文章主要介紹了Android自定義圓形進(jìn)度條實(shí)現(xiàn)代碼,進(jìn)度條在Android中教程經(jīng)常使用到,本文向大家分享了Android實(shí)現(xiàn)自定義圓形進(jìn)度條的代碼,感興趣的小伙伴們可以參考一下2016-03-03Android開發(fā)之簡單文件管理器實(shí)現(xiàn)方法
這篇文章主要介紹了Android開發(fā)之簡單文件管理器實(shí)現(xiàn)方法,簡單實(shí)現(xiàn)了Android的文件目錄查看,文件重命名,打開,刪除等功能,需要的朋友可以參考下2016-01-01Android自定義View實(shí)現(xiàn)shape圖形繪制
這篇文章主要為大家詳細(xì)介紹了Android使用自定義View實(shí)現(xiàn)shape圖形繪制,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01Android開發(fā)環(huán)境搭建圖文教程 親測(cè)有效!
這篇文章主要為大家詳細(xì)介紹了Android開發(fā)環(huán)境搭建圖文教程,親自測(cè)試有效的搭建方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03Android進(jìn)階Hook攔截系統(tǒng)實(shí)例化View過程實(shí)現(xiàn)App換膚功能
這篇文章主要為大家介紹了Android進(jìn)階Hook攔截系統(tǒng)實(shí)例化View過程實(shí)現(xiàn)App換膚功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01Flutter之Timer實(shí)現(xiàn)短信驗(yàn)證碼獲取60s倒計(jì)時(shí)功能的代碼
這篇文章主要介紹了Flutter之Timer實(shí)現(xiàn)短信驗(yàn)證碼獲取60s倒計(jì)時(shí)功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07Android程序自動(dòng)更新功能模塊的實(shí)現(xiàn)方法【附完整demo源碼下載】
這篇文章主要介紹了Android程序自動(dòng)更新功能模塊的實(shí)現(xiàn)方法,具備完整的自動(dòng)檢測(cè)更新及下載、安裝等功能,并附帶完整的demo源碼供大家下載參考,需要的朋友可以參考下2016-08-08Android實(shí)現(xiàn)界面左右滑動(dòng)切換功能
相信大家一定都使用過手機(jī)QQ和微信之類的軟件,當(dāng)我們使用時(shí)不難發(fā)現(xiàn)其界面的切換不僅可以通過點(diǎn)擊頁標(biāo)簽來實(shí)現(xiàn),還可以通過左右滑動(dòng)來實(shí)現(xiàn)的,下面小編給大家介紹下如何實(shí)現(xiàn)這個(gè)功能2016-12-12