Android自定義狀態欄通知(Status Notification)的正確實現

在Android應用開發中,常常會使用到狀態欄通知(Status Notification),例如新浪微博、網易新聞等提供的推送消息,軟件後臺更新時進度的顯示等等,以下圖所示: android

看了網上不少關於Notification的博客文章,發現幾乎沒有一個能將自定義狀態欄通知徹底實現正確的,所以,本文就來講說實現自定義狀態欄通知常常被忽略的一些知識點。 app


1) 使用Notification最多見的場景 佈局

運行在後臺的Service當須要和用戶交互時,因爲它不能直接啓動一個Activity,所以,Service必須使用Notification來間接的啓動Activity(當用戶點擊Notification時跳轉)。 字體


2) 自定義佈局文件支持的控件類型 spa

Notification的自定義佈局是RemoteViews,所以,它僅支持FrameLayout、LinearLayout、RelativeLayout三種佈局控件,同時支持AnalogClock、Chronometer、Button、ImageButton、ImageView、ProgressBar、TextView、ViewFlipper、ListView、GridView、StackView和AdapterViewFlipper這些UI控件。對於其餘不支持的控件,使用時將會拋出ClassNotFoundException異常。 xml


3) Notification支持的Intent類型(都是PendingIntent類的實例) 事件

contentIntent:在通知窗口區域,Notification被單擊時的響應事件由該intent觸發; ip

deleteIntent:在通知窗口區域,當用戶點擊所有清除按鈕時,響應該清除事件的Intent; utf-8

fullScreenIntent:響應緊急狀態的全屏事件(例如來電事件),也就是說通知來的時候,跳過在通知區域點擊通知這一步,直接執行fullScreenIntent表明的事件。 開發

上面三種PendingIntent能夠拉起Activity、Service和BroadcastReceiver,如圖所示:


4) 狀態欄通知字體的設置

不一樣的手機,不一樣的Android平臺版本,狀態欄通知窗口的背景顏色可能千差萬別,例如Android2.3以前的版本通知窗口默認背景是白色的,Android4.0以後的版本通知窗口背景默認是黑色的,這就須要在設置Notification的字體時加以區別,不然,很容易致使通知的字體顏色和背景色同樣,從而看不到字體部分,市面上不少app就存在這個問題。以下圖所示,華爲智匯雲和百度音樂這兩款應用就明顯存在這個問題。


從Android2.3(API level 9)開始,系統爲默認通知欄佈局的字體定義了樣式以下:

"android:TextAppearance.StatusBar.EventContent"

"android:TextAppearance.StatusBar.EventContent.Title"

所以,在2.3以後的版本中咱們自定義佈局文件中的字體直接應用這個樣式就能夠。對於2.3以前的版本,因爲背景色是白色的,所以,咱們使用以下系統預約義樣式指定字體的顏色:

?android:attr/textColorPrimary

所以,在res的values目錄下定義styles.xml文件以下:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <style name="NotificationText">  
  5.         <item name="android:textColor">?android:attr/textColorPrimary</item>  
  6.     </style>  
  7.   
  8.     <style name="NotificationTitle">  
  9.         <item name="android:textColor">?android:attr/textColorPrimary</item>  
  10.         <item name="android:textStyle">bold</item>  
  11.     </style>  
  12.   
  13. </resources>  

在res的values-v9目錄下定義styles.xml文件以下:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <style name="NotificationText" parent="android:TextAppearance.StatusBar.EventContent" />  
  5.   
  6.     <style name="NotificationTitle" parent="android:TextAppearance.StatusBar.EventContent.Title" />  
  7.   
  8. </resources>  

自定義通知佈局文件使用styles文件以下:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:id="@+id/layout"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     tools:ignore="ContentDescription" >  
  7.   
  8.     <ImageView  
  9.         android:id="@+id/image"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_alignParentLeft="true"  
  13.         android:layout_centerVertical="true"  
  14.         android:layout_marginLeft="5.0dp"  
  15.         android:layout_marginRight="10.0dp" />  
  16.   
  17.     <RelativeLayout  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:layout_centerVertical="true"  
  21.         android:layout_toRightOf="@id/image" >  
  22.   
  23.         <TextView  
  24.             android:id="@+id/title"  
  25.             style="@style/NotificationTitle"  
  26.             android:layout_width="wrap_content"  
  27.             android:layout_height="wrap_content" />  
  28.   
  29.         <TextView  
  30.             android:id="@+id/text"  
  31.             style="@style/NotificationText"  
  32.             android:layout_width="wrap_content"  
  33.             android:layout_height="wrap_content"  
  34.             android:layout_below="@id/title" />  
  35.     </RelativeLayout>  
  36.   
  37. </RelativeLayout>
相關文章
相關標籤/搜索