用慣了Android的人在剛拿到iPhone的時候,總是會習慣性的用手指從狀態欄往下拖一下,這都是給Notification鬧的。
不過Notification也確實是1個不錯的提示工具,不干擾正常的操作,事後還可以再翻看詳細的內容,點擊後還可以進入相關的畫面查看更具體的內容。
今天我就以代碼爲主的形式來介紹Notification的使用,包括基本用法,自定義的View,以及更多的控制方法。
另一種Android中常用到的提示方法Toast的用法請參見《教程:在Android中使用Toast進行提示》
我們先看下Notification的幾個主要組成部分:
Icon:不解釋
Ticker Text:Notification剛出來的時候,在狀態欄上滾動的字幕,如果很長,會自動分割滾動
Content Title:Notification展開後的標題
Content Text:Notification展開後的內容
Notification的一般用法
取得NotificationManager
1 2 3 |
|
創建Notification並且顯示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
這是最基本的應用,可以說除了找個合適的圖標以外,其它都很簡單。
使用自定義View的Notification
同Toast一樣,我們也可以自已指定1個View來作爲Notification展開後的顯示內容,比如說在Android Market中下載的時候,Notification中會顯示當前下載的進度,那麼我們也來模擬1個這樣的效果吧。
首先給出View的定義文件:notification_view_sample.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="3dp" > <ImageView android:id="@+id/notificationImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@android:drawable/stat_sys_download" /> <TextView android:id="@+id/notificationTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/notificationImage" android:layout_alignParentRight="true" android:paddingLeft="6dp" android:textColor="#FF000000" /> <TextView android:id="@+id/notificationPercent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/notificationImage" android:paddingTop="2dp" android:textColor="#FF000000" /> <ProgressBar android:id="@+id/notificationProgress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/notificationTitle" android:layout_alignLeft="@id/notificationTitle" android:layout_alignParentRight="true" android:layout_alignTop="@id/notificationPercent" android:paddingLeft="6dp" android:paddingRight="3dp" android:paddingTop="2dp" style="?android:attr/progressBarStyleHorizontal" /> </RelativeLayout> |
RelativeLayout的使用,可以參考:《教程:Android各種Layout特性和使用匯總(一)》
接下來是Java代碼片段:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
注意以上代碼中使用的是RemoteViews,而不是普通的View,另外使用的是PendingIntent而不是普通的Intent,這都說明了Notification是1個“遠程”的東西,其中能夠使用的控件是受限制的,比如說TableLayout就不能使用。看下效果圖,是不是和Market中的界面很接近呢?
更好的控制Notification
動畫圖標怎麼做?
和selector類似,定義1個XML文件放在drawable下,下面是之前用到的stat_sys_download的定義:
1 2 3 4 5 6 7 8 9 10 |
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/stat_sys_download_anim0" android:duration="200" /> <item android:drawable="@drawable/stat_sys_download_anim1" android:duration="200" /> <item android:drawable="@drawable/stat_sys_download_anim2" android:duration="200" /> <item android:drawable="@drawable/stat_sys_download_anim3" android:duration="200" /> <item android:drawable="@drawable/stat_sys_download_anim4" android:duration="200" /> <item android:drawable="@drawable/stat_sys_download_anim5" android:duration="200" /> </animation-list> |
如何更新Notification?
注意到前面的代碼中用到的CUSTOM_VIEW_ID,這是Notification的ID,如果2次彈出的Notification的ID相同,那麼Notification就只會更新而不會再次滾動提醒。之前給出的ProgressBar是不會動的,利用這個方法就可以讓它動起來(或者也可以直接調用RemoteView的set方法來直接更新?未試驗)
如何自定義提示的聲音和振動?
//自定義提示音 notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3"); //自定義振動方式 long[] vibrate = {0,100,200,300}; notification.vibrate = vibrate;
請注意:如果使用了DEFAULT_SOUND或DEFAULT_VIBRATE,則自定義的提示音和振動無效。
在類似於短消息的應用中如何提示數量?
使用Notification的number屬性,默認爲0,如果是1或更大的數字,則會在圖標上覆蓋顯示這個數字。
notification.number=notificationNumber;
Flag的使用
notification有1個flag屬性,除了DEFAULT_SOUND之外,還有幾個很有用的屬性。
FLAG_AUTO_CANCEL:自動清除Notification,前面的例子中有用到
FLAG_INSISTENT:提示音一直不停,直至用戶響應(很吵吧!)
FLAG_ONGOING_EVENT:表示這是1個正在進行的任務,不可以清除,第2個例子中有用到
FLAG_NO_CLEAR:不可以清除
© 2011, Bing. 版權所有。 所有轉載請以鏈接方式進行。