Android API Level在11前后及16之后時Notification的不同用法


作為剛入門Android的小白,最近在按照郭大神的《第一行代碼》在練習,在用到Notification時遇到了一些問題,網上資料比較零散,我這里做了一個總結分析給各位,若有錯誤,懇請指正~

Notification是一種具有全局效果的通知,程序一般通過NotificationManager服務來發送Notification

Notification支持文字內容顯示、震動、三色燈、鈴聲等多種提示形式,在默認情況下,Notification僅顯示消息標題、消息內容、送達時間這3項內容。

以下就是通知的基本布局:

不同API level的區別主要是Notification的構造方法、得到實例的方法,這里順便總結一下Notification的用法,按照步驟分別給出不同API level下的做法:

1、獲取Notification管理器

NotificationManager noteManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

獲取管理器的方式都一樣,沒有改變。

2、新建一個Notification,設置狀態欄顯示樣式

這里只列舉了最簡單的樣式設置,具體的大家可以參考API文檔。

// API Level < 11 (Android 3.0)
Notification notification = new Notification(
		   R.mipmap.ic_launcher, "This is ticker text",
		   System.currentTimeMillis());

API<11時,可以直接用Notification的構造方法新建一個Notification,十分方便。

  ps:我用的IDE是AndroidStudio,所以icon的id是R.mipmap.***。

// API Level >= 11 (Android 3.0) && API Level < 16 (Android 4.1)
Notification.Builder builder = new Notification.Builder(this)
                     .setSmallIcon(R.mipmap.ic_launcher)
                     .setTicker("This is ticker text")
                     .setWhen(System.currentTimeMillis());
Notification note = builder.getNotification(); // 調用getNotification()來生成Notification

API>11后,就要用Notification.Builder()來代替了,官方API是這樣說的:

  Notification(int icon, CharSequence tickerText, long when)

  This constructor was deprecated in API level 11. Use Notification.Builder instead.
 
那就沒有統一的方案嗎?當然有了
 
我在介紹Notification.Builder的API網頁上看到這樣一段話:
 
  If your app supports versions of Android as old as API level 4, you can instead use  NotificationCompat.Builder, available in the  Android Support library.
 
這說明只要API>4,都可以用NotificationCompat.Builder,實踐證明也確實可行:
 
//API Level >= 4 (Android 1.6) && API Level < 16 (Android 4.1)
NotificationCompat.Builder builder = new NotificationCompat.Builder(this) 
            .setSmallIcon(R.mipmap.ic_launcher) 
            .setTicker("This is ticker text") 
            .setWhen(System.currentTimeMillis());
Notification note =builder.getNotification(); //調用builder.getNotification()來生成Notification

因此推薦用NotificationCompat.Builder這種方式。

注意到我在注釋里寫的 "&& API Level < 16 (Android 4.1)" 了嗎?這是因為在Google官方API文檔上是這么說的:

  public Notification getNotification ()    Added in API level 11

    This method was deprecated in API level 16.

    Use build() instead

因此,當API>=16即Android4.1之后,就要用build()來代替了,使用方法一樣,在此就不贅述了。

3、設置Notification的觸發事件

點擊Notification后,一般都是觸發一個新的Activity:

Intent intent = new Intent(this, AnotherActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent,
                  PendingIntent.FLAG_CANCEL_CURRENT);    

這里不同API版本都一樣。

4、設置Notification在通知欄里的樣式

// API Level < 11 (Android 3.0)
note.setLatestEventInfo(this, "This is content title", "This is content text", pi);

API<11時,調用的是Notification類的setLatestEventInfo方法。

// API Level >= 11 (Android 3.0)
builder.setContentIntent(pi)
                 .setContentTitle("This is content title")
                 .setContentText("This is content text");

而當API>=11時,設置通知欄中的樣式是調用Builder的setContentIntent方法,設置好之后在調用build()方法生成Notification實例。

類似的,NotificationCompat.Builder也是調用同樣的方法,這里就不贅述了。

5、發布該Notification

第一個參數為該notification的ID

noteManager.notify(1, note);

總結一下:

低版本(API低於11、16)中的部分方法已經被棄用:

 (1)Notification.Builder(this).getNotification()

 (2)mNotification.setLatestEventInfo(this, "title", "content", null);  

建議開發過程中盡量使用NotificationCompat.Builder(this)的構建方法去創建一個通知類。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM