Notification 使用詳解(很全)_第1頁(yè)
Notification 使用詳解(很全)_第2頁(yè)
Notification 使用詳解(很全)_第3頁(yè)
Notification 使用詳解(很全)_第4頁(yè)
Notification 使用詳解(很全)_第5頁(yè)
已閱讀5頁(yè),還剩4頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、Notification 使用詳解(很全)Java & android2011-01-25 14:23:21當(dāng)用戶有沒(méi)有接到的電話的時(shí)候,Android頂部狀態(tài)欄里就會(huì)出現(xiàn)一個(gè)小圖標(biāo)。提示用戶有沒(méi)有處理的快訊,當(dāng)拖動(dòng)狀態(tài)欄時(shí),可以查看這些快訊。Android給我們提供了NotificationManager來(lái)管理這個(gè)狀態(tài)欄??梢院茌p松的完成。 如果要添加一個(gè)Notification,可以按照以下幾個(gè)步驟1:獲取NotificationManager:NotificationManager m_NotificationManager=(NotificationManager)this.getSy

2、stemService(NOTIFICATION_SERVICE);2:定義一個(gè)Notification:Notificationm_Notification=new Notification();3:設(shè)置Notification的各種屬性:/設(shè)置通知在狀態(tài)欄顯示的圖標(biāo)m_Notification.icon=R.drawable.icon; /當(dāng)我們點(diǎn)擊通知時(shí)顯示的內(nèi)容m_Notification.tickerText=Button1 通知內(nèi)容.; 通知時(shí)發(fā)出的默認(rèn)聲音m_Notification.defaults=Notification.DEFAULT_SOUND;/設(shè)置通知顯示的參數(shù)In

3、tent m_Intent=new Intent(NotificationDemo.this,DesActivity.class); PendingIntent m_PendingIntent=PendingIntent.getActivity(NotificationDemo.this, 0, m_Intent, 0);m_Notification.setLatestEventInfo(NotificationDemo.this, Button1, Button1通知,m_PendingIntent );/這個(gè)可以理解為開(kāi)始執(zhí)行這個(gè)通知m_NotificationManager.notify

4、(0,m_Notification);4:既然可以增加同樣我們也可以刪除。當(dāng)然是只是刪除你自己增加的。m_NotificationManager.cancel(0); 這里的0是一個(gè)ID號(hào)碼,和notify第一個(gè)參數(shù)0一樣。這也就完成了,添加刪除工作。這里我們還是一個(gè)Demo來(lái)掩飾我們的操作。1:新建一個(gè)工程N(yùn)otificationDemo。2:添加一個(gè)Activity:DesActivity,注意需要在Manifest.xml中聲明3:NotificationDemo中的Laytout文件很簡(jiǎn)單就是定義一個(gè)Button.其代碼文件如下:1. package com.rocky.studio.

5、ch4221;2. import android.app.Activity;3. import android.app.Notification;4. import android.app.NotificationManager;5. import android.app.PendingIntent;6. import android.content.Intent;7. import android.os.Bundle;8. import android.view.View;9. import android.widget.Button;10. import android.widget.Te

6、xtView;11. public class NotificationDemo extends Activity 12.13. Button m_Button1;14. TextView m_txtView;15.16. NotificationManager m_NotificationManager;17. Notification m_Notification;18.19. Intent m_Intent;20. PendingIntent m_PendingIntent;21. 22. /* Called when the activity is first created. */2

7、3. Override24. public void onCreate(Bundle savedInstanceState) 25. super.onCreate(savedInstanceState);26. setContentView(R.layout.main);27. 28. m_NotificationManager=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);29. 30. 31. m_Button1=(Button)this.findViewById(R.id.Button01);32. 33

8、. 34. /點(diǎn)擊通知時(shí)轉(zhuǎn)移內(nèi)容35. m_Intent=new Intent(NotificationDemo.this,DesActivity.class);36. 37. m_PendingIntent=PendingIntent.getActivity(NotificationDemo.this, 0, m_Intent, 0);38. 39. m_Notification=new Notification();40. 41. m_Button1.setOnClickListener(new Button.OnClickListener()42. public void onClick

9、(View v) 43. / TODO Auto-generated method stub44. 45. /設(shè)置通知在狀態(tài)欄顯示的圖標(biāo)46. m_Notification.icon=R.drawable.icon;47. 48. /當(dāng)我們點(diǎn)擊通知時(shí)顯示的內(nèi)容49. m_Notification.tickerText=Button1 通知內(nèi)容.;50. 51. /通知時(shí)發(fā)出的默認(rèn)聲音52. m_Notification.defaults=Notification.DEFAULT_SOUND;53. 54. /設(shè)置通知顯示的參數(shù)55. m_Notification.setLatestEventI

10、nfo(NotificationDemo.this, Button1, Button1通知,m_PendingIntent );56. 57. /這個(gè)可以理解為開(kāi)始執(zhí)行這個(gè)通知58. m_NotificationManager.notify(0,m_Notification);59. 60. );61. 62. 63. 64. 65. 復(fù)制代碼4:修改DesActivity 的源文件,代碼如下:它做的事情就是取消之前添加的Notification1. package com.rocky.studio.ch4221;2. import android.app.Activity;3. import

11、 android.app.NotificationManager;4. import android.os.Bundle;5. public class DesActivity extends Activity 6.7. NotificationManager m_NotificationManager;8.9. Override10. protected void onCreate(Bundle savedInstanceState) 11. / TODO Auto-generated method stub12. super.onCreate(savedInstanceState);13.

12、 this.setContentView(R.layout.main2);14. 15. /啟動(dòng)后刪除之前我們定義的16. m_NotificationManager=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);17. m_NotificationManager.cancel(0); 18. 19.20. 復(fù)制代碼代碼也很簡(jiǎn)單??梢圆榭碞otification , NotificationMananger 這兩個(gè)類來(lái)學(xué)習(xí)前后左右。下面是一篇文章,對(duì)Notification ,NotificationManag

13、er這兩個(gè)類有詳細(xì)的說(shuō)明介紹,特借鑒一下。NoticificationManager很容易可以放在狀態(tài)欄,也很容易實(shí)現(xiàn)從statusbar進(jìn)入程序 中,NoticificationManager中通過(guò)intent執(zhí)行此程序的activity就可以了NoticificationManager狀態(tài)欄操作NotificationManager(通知管理器):NotificationManager負(fù)責(zé)通知用戶事件的發(fā)生.NotificationManager有三個(gè)公共方法:1. cancel(int id) 取消以前顯示的一個(gè)通知.假如是一個(gè)短暫的通知,試圖將隱藏,假如是一個(gè)持久的通知,將從狀態(tài)條中移

14、走.2. cancelAll() 取消以前顯示的所有通知.3. notify(int id,Notification notification) 把通知持久的發(fā)送到狀態(tài)條上./初始化NotificationManager:NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);Notification代表著一個(gè)通知.Notification的屬性:audioStreamType 當(dāng)聲音響起時(shí),所用的音頻流的類型contentIntent 當(dāng)通知條目被點(diǎn)擊,就執(zhí)行這個(gè)被設(shè)置的Inte

15、nt.contentView 當(dāng)通知被顯示在狀態(tài)條上的時(shí)候,同時(shí)這個(gè)被設(shè)置的視圖被顯示.defaults 指定哪個(gè)值要被設(shè)置成默認(rèn)的.deleteIntent 當(dāng)用戶點(diǎn)擊Clear All Notifications按鈕區(qū)刪除所有的通知的時(shí)候,這個(gè)被設(shè)置的Intent被執(zhí)行.icon 狀態(tài)條所用的圖片.iconLevel 假如狀態(tài)條的圖片有幾個(gè)級(jí)別,就設(shè)置這里.ledARGB LED燈的顏色.ledOffMS LED關(guān)閉時(shí)的閃光時(shí)間(以毫秒計(jì)算)ledOnMS LED開(kāi)始時(shí)的閃光時(shí)間(以毫秒計(jì)算)number 這個(gè)通知代表事件的號(hào)碼sound 通知的聲音tickerText 通知被顯示在狀態(tài)條

16、時(shí),所顯示的信息vibrate 振動(dòng)模式.when 通知的時(shí)間戳.將Notification發(fā)送到狀態(tài)條上:Notification notification = new Notification();Notification的設(shè)置過(guò)程.nm.notify(0, notification); /發(fā)送到狀態(tài)條上創(chuàng)建和觸發(fā)Notification創(chuàng)建和配置新的Notification需要經(jīng)歷三步。 首先,你要?jiǎng)?chuàng)建一個(gè)新的Notification對(duì)象,傳入要在狀態(tài)條上顯示的圖 標(biāo)、文字和Notification的 當(dāng)前時(shí)間,如下面的代碼片段所示: / Choose a drawable to dis

17、play as the status bar iconint icon = R.drawable.icon; / Text to display in the status bar when the notification is launchedString tickerText = “Notification”; / The extended status bar orders notification in time orderlong when = System.currentTimeMillis();Notification notification = new Notificati

18、on(icon, tickerText, when); 當(dāng)Notification觸發(fā)時(shí),文本將沿著狀態(tài)條進(jìn)行滾動(dòng) 顯示。其次,使用setLatestEventInfo方法來(lái)配置Notification在擴(kuò)展的狀態(tài)窗口中的外觀。擴(kuò)展的狀態(tài)窗口將顯示圖標(biāo)和在構(gòu)造函數(shù)中傳入的時(shí)間,以及顯示標(biāo)題和一個(gè)詳細(xì)的字符串。Notification一般表示為對(duì)一個(gè)動(dòng)作的請(qǐng)求或引起用戶的注意,所以,當(dāng)用戶點(diǎn)擊Notification項(xiàng)目時(shí)你可以指定一個(gè)PendingIntent來(lái)觸發(fā)。 下面的代碼片段使用了setLastestEventInfo來(lái)設(shè)置這些值: Context context = getAppli

19、cationContext(); / Text to display in the extended status windowString expandedText = “Extended status text”; / Title for the expanded statusString expandedTitle = “Notification Title”; / Intent to launch an activity when the extended text is clickedIntent intent = new Intent(this, MyActivity.class)

20、;PendingIntent launchIntent = PendingIntent.getActivity(context, 0, intent, 0);notification.setLatestEventInfo(context,expandedTitle,expandedText,launchIntent); 一個(gè)好的形式是顯示相同事件(例如,接 收多個(gè)SMS消息)的多個(gè)對(duì)象時(shí) 使用一個(gè)Notification圖 標(biāo)。為了呈現(xiàn)給用戶,使用setLastestEventInfo更新數(shù)據(jù)集來(lái)呈現(xiàn)最近的消息以及重新觸發(fā)Notification來(lái)更新它的值。 你還可以使用number屬性來(lái)顯

21、示一個(gè)狀態(tài)條圖標(biāo)所表示的事件數(shù)目。 設(shè)置為比1大的數(shù),如下所示,將在狀態(tài)條圖標(biāo)上以一個(gè)小小的數(shù)字進(jìn)行 顯示: notification.number+; 任何對(duì)Notification的變更,你都需要重新觸發(fā)來(lái)進(jìn)行更 新。如果要?jiǎng)h除這個(gè)數(shù)字,設(shè)置number的值為0或者-1。 最后,你可以對(duì)Notification對(duì)象使用多種屬性來(lái)增強(qiáng)Notification的效果,如閃爍LED、震動(dòng)電話和播放音樂(lè)文件。這些高級(jí)特征將在本章的后面部分進(jìn)行詳細(xì)描述。 觸發(fā)Notification 為了觸發(fā)一個(gè)Notification,使用NotificationManager的notify方法,將一個(gè)整數(shù)的ID

22、和Notification對(duì)象傳入,如下的片段所示: int notificationRef = 1;notificationManager.notify(notificationRef, notification); 為了更新一個(gè)已經(jīng)觸發(fā)過(guò)的Notification,傳入相同的ID。你既可以傳入相同的Notification對(duì)象,也可以是一個(gè)全新的對(duì)象。只 要ID相同,新的Notification對(duì)象會(huì)替換狀態(tài)條圖標(biāo)和擴(kuò)展的狀態(tài)窗口的細(xì)節(jié)。 你還可以使用ID來(lái)取消Notification,通過(guò)調(diào)用NotificationManager的cancel方法,如下所示: notificationM

23、anager.cancel(notificationRef); 取消一個(gè)Notification時(shí),將移除它的狀態(tài)條圖標(biāo)以及清除 在擴(kuò)展的狀態(tài)窗口中的信息。Notification和NotificationManager的基本使用方法1. NotificationManager和Notification用來(lái)設(shè)置通知。通知的設(shè)置等操作相對(duì)比較簡(jiǎn)單,基本的使用方式就是用新建一個(gè)Notification對(duì)象,然后設(shè)置好通知的各項(xiàng)參數(shù),然后使用系統(tǒng)后臺(tái)運(yùn)行的 NotificationManager服務(wù)將通知發(fā)出來(lái)?;静襟E如下:1)得到NotificationManager:String ns = C

24、ontext.NOTIFICATION_SERVICE;NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);2)創(chuàng)建一個(gè)新的Notification對(duì)象:Notification notification = new Notification();notification.icon = R.drawable.notification_icon;也可以使用稍微復(fù)雜一些的方式創(chuàng)建Notification:int icon = R.drawable.notification_ico

25、n; /通知圖標(biāo)CharSequence tickerText = Hello;/狀態(tài)欄(Status Bar)顯示的通知文本提示long when = System.currentTimeMillis(); /通知產(chǎn)生的時(shí)間,會(huì)在通知信息里顯示Notification notification = new Notification(icon, tickerText, when);3)填充Notification的各個(gè)屬性:Context context = getApplicationContext();CharSequence contentTitle = My notification;CharSequence contentText = Hello World!;Intent notificationIntent = new Intent(this, MyClass.class);PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);notification.setLatestEventInfo(context

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論