天天看點

Android系列教程(1):Notification 與狀态欄資訊

本文為《Android/OPhone 開發完全講義》的内容連載,如需轉載,請注明作者和出處,謝謝!

<a target="_blank" href="http://www.cnblogs.com/nokiaguy/archive/2010/07/01/1769079.html">源代碼下載下傳</a>

    Notification 與 Toast 都可以起到通知、提醒的作用。但它們的實作原理和表現形式卻完全不一樣。 Toast 其實相當于一個元件( Widget )。有些類似于沒有按鈕的對話 框。而 Notification 是顯示在螢幕上方狀态欄中的資訊。還有就是 Notification 需要用 NotificationManager 來管理,而 Toast 隻需要簡單地建立 Toast 對象即可。

下面來看一下建立并顯示一個 Notification 的步驟。建立和顯 示一個 Notification 需要如下 5 步:

1.  通過 getSystemService 方法獲得一個 NotificationManager 對象。

2.  建立一個 Notification 對象。每一個 Notification 對應一個 Notification 對象。在這一步需要設定顯示在螢幕上方狀态欄的通知消息、通知消息前方的圖像資源 ID 和發出通知的時間。一般為目前時間。

3.  由于 Notification 可以與應用程式脫離。也就是說,即使應用程式被關閉, Notification 仍然會顯示在狀态欄 中。當應用程式再次啟動後,又可以重新控制這些 Notification 。如清除或替換它們。是以,需要建立一個 PendingIntent 對象。該對象由 Android 系統負責維護,是以,在應用程式關閉後,該對象仍然不會被釋放。

4.  使用 Notification 類的 setLatestEventInfo 方法設定 Notification 的詳細資訊。

5.  使用 NotificationManager 類的 notify 方法顯示 Notification 消息。在這一步需要指定辨別 Notification 的唯一 ID 。這個 ID 必須相對于同一個 NotificationManager 對象是唯一的,否則就會覆寫相同 ID 的 Notificaiton 。

心動不如行動,下面我們來演練一下如何在狀 态欄顯示一個 Notification ,代碼如下:

    上面的 5 行代碼正好對應建立和顯示 Notification 的 5 步。在這裡要解釋一下的是 notify 方法的第 1 個參數。這個參數實際上表示了 Notification 的 ID 。是一個 int 類型的值。為了使這個值唯一,可以使用 res 目錄中的某些資源 ID 。例如,在上面的代碼中使用了目前 Notification 顯示的圖像對應的 資源 ID ( R.drawable.icon )作為 Notification 的 ID 。當然,讀者也可以使用其他的值作為 Notification 的 ID 值。

     由于建立和顯示多個 Notification 的代碼類似,因 此,在本節的例子中編寫了一個 showNotification 方法來顯示 Notification ,代碼如下:

private void showNotification(String tickerText, String contentTitle, String contentText, int id, int resId)  

{  

    Notification notification = notification = new Notification(resId, tickerText, System.currentTimeMillis());  

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, getIntent(), 0);  

    notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);  

    //  notificationManager是在類中定義 的 NotificationManager變量。在onCreate方法中已經建立  

    notificationManager.notify(id, notification);  

}  

private void showNotification(String tickerText, String contentTitle, String contentText, int id, int resId) { Notification notification = notification = new Notification(resId, tickerText, System.currentTimeMillis()); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, getIntent(), 0); notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent); // notificationManager是在類中定義的 NotificationManager變量。在onCreate方法中已經建立 notificationManager.notify(id, notification); }

下面的代碼使用 showNotification 方法顯示了 3 個 Notification 消息。

showNotification("今天非常高興", "今天考試 得了全年級第一",  

        "數學100分、國文99分、英語100分,yeah!", R.drawable.smile, R.drawable.smile);  

showNotification("這是為什麼呢?", "這道題為什麼會出錯呢?", " 誰有正确答案啊.",  

        R.drawable.why, R.drawable.why);  

showNotification("今天心情不好", "也不知道為什麼,這幾天一直很郁悶.", "也許應該去公園散心了",  

        R.drawable.why, R.drawable.wrath);  

showNotification(" 今天非常高興", "今天考試得了全年級第一", "數學100分、國文99分、英語100分,yeah!", R.drawable.smile, R.drawable.smile); showNotification("這是為什麼呢?", "這道題為什麼會出錯呢?", "誰有正确答案啊.", R.drawable.why, R.drawable.why); showNotification("今天心情不好", "也不知道為什麼,這幾天一直很郁悶.", "也許應該去公園散心了", R.drawable.why, R.drawable.wrath);

    其中第 2 個和第 3 個 Notification 使用的是同一個 ID ( R.drawabgle.why ),是以,第 3 個 Notification 會覆寫第 2 個 Notification 。 在顯示 Notification 時還可以設定顯示通 知時的預設發聲、震動和 Light 效果。要實作這個功能需要設定 Notification 類的 defaults 屬性,代碼如下:

notification.defaults = Notification.DEFAULT_SOUND;        //  使用預設的聲音  

notification.defaults = Notification.DEFAULT_VIBRATE;        //  使用預設的震動  

notification.defaults = Notification.DEFAULT_LIGHTS;        //  使用預設的Light  

notification.defaults = Notification.DEFAULT_ALL;            //  所有的都使用預設值  

notification.defaults = Notification.DEFAULT_SOUND; // 使用預設的聲音 notification.defaults = Notification.DEFAULT_VIBRATE; // 使用預設的震動 notification.defaults = Notification.DEFAULT_LIGHTS; // 使用預設的Light notification.defaults = Notification.DEFAULT_ALL; // 所有的都使用預設值

   注意:設定預設發聲、震動和 Light 的方法是 setDefaults 。該 方法與 showNotification 方法的實作代碼基本相同,隻是在調用 notify 方法之前需要設定 defaults 屬性( defaults 屬性必須在 調用 notify 方法之前調用,否則不起作用)。在設定預設震動效果時還需要在 AndroidManifest.xml 檔案中通過 &lt;uses-permission&gt; 标簽設定 android.permission.VIBRATE 權限。

    如果要清除某個消息,可以使用 NotificationManager 類 的 cancel 方 法,該方法隻有一個參數,表示要清除的 Notification 的 ID 。使用 cancelAll 可以清除目前 NotificationManager 對象中的所有 Notification 。

    運作本節的例子,單擊螢幕上顯示 Notification 的按鈕,會顯示如 圖1 所示的消息。每一個消息會顯示一會,然後就隻顯示整個 Android 系統(也包括其他應用程式) 的 Notification (隻顯示圖像部分)。如圖2 所示。如果将狀态欄拖下來,可以看 到 Notification 的詳細資訊和發出通知的時間(也就是 Notification 類的構造方法的第 3 個參數值),如圖3 所 示。當單擊【清除通知】按鈕,會清除本應用程式顯示的所有 Notification ,清除後的效果如圖4 所示。

Android系列教程(1):Notification 與狀态欄資訊

                  圖1

Android系列教程(1):Notification 與狀态欄資訊

                    圖2

Android系列教程(1):Notification 與狀态欄資訊

                  圖3

Android系列教程(1):Notification 與狀态欄資訊

                 圖4

繼續閱讀