狀態通知欄

在android的應用層中,涉及到不少應用框架,例如:Service框架,Activity管理機制,Broadcast機制,對話框框架,標題欄框架,狀態欄框架,通知機制,ActionBar框架等等。html

下面就來講說常常會使用到通知機制中的通知欄框架(Notificaiton),它適用於交互事件的通知。它是位於頂層能夠展開的通知列表。它會時不時的提醒你什麼軟件該更新了,什麼人發你微信消息了等。java

(網上看了下,全面介紹的文章很少,因此就萌生了寫這篇的念頭,隨便看成回顧筆記。下面我就經過官方文檔、源代碼、書上的一些資料彙總下這一塊的知識,並經過一個通知欄的彙總DEMO讓你們更好的瞭解這個類的使用,內容有點多,能夠根據需求看目錄學習)。android


Notificaiton狀態通知欄:git


功能做用


1.顯示接收到短消息、即便消息等信息 (如QQ、微信、新浪、短信)  
2.顯示客戶端的推送消息(若有新版本發佈,廣告,推薦新聞等) 
3.顯示正在進行的事物(例如:後臺運行的程序)(如音樂播放器、版本更新時候的下載進度等)
github


思惟導圖結構


思惟導圖的大致結構(按照各個節點延伸拓展學習)微信

Notificaiton -- service   -- BroadcastReceiver  -- Intent(flag、Action等屬性應用) --  PendingIntent
網絡

感慨:app

一個Notificaiton通知的拓展使用就要涉及與4大組建的配合,因此學好總體的知識體系。
框架

聯繫:ide

1.因爲service 是在後臺運行,因此它意圖作什麼咱們看不到,能夠經過Notificaiton 來顯示提醒(如音樂的後臺播放)。

2.service服務和BroadcastReceiver廣播相結合,在加上Notificaiton 顯示(如程序的後臺更新)。

3.Intent做爲意圖處理,和Notificaiton的點擊時間緊密結合在了一塊兒,而且與BroadcastReceiver和service的聯繫也緊密不能夠分割。

service 在後臺以後經過BroadcastReceiver來通知Notificaiton 顯示相關東西,在經過Intent完成用戶的意圖操做)

相關文檔:Activity啓動模式 及 Intent Flags 與 棧 的關聯分析


對應的官方連接

 


設計文檔 :   
使用教程 :  

開發文檔 :http://developer.android.com/reference/android/app/Notification.html


大致瞭解


Notification支持文字內容顯示、震動三色燈鈴聲等多種提示形式,在默認狀況下,Notification僅顯示消息標題消息內容送達時間這3項內容。如下就是通知的基本佈局。

 

普通視圖:

高度64dp

大試圖的通知在展開前也顯示爲普通視圖

元素:

 

1. 標題   Title/Name

2大圖標  Icon/Photo

3. 內容文字   

4. 內容信息   MESSAGE

5. 小圖標 Secondary Icon

6. 通知的時間 Timestamp,默認爲系統發出通知的時間,也可經過setWhen()來設置



相關分析


狀態通知欄主要涉及到2個類:  Notification 和 NotificationManager 

Notification爲通知信息類,它裏面對應了通知欄的各個屬性

NotificationManager :  是狀態欄通知的管理類,負責發通知、清除通知等操做。

注意:NotificationManager 是一個系統Service,因此必須經過 getSystemService(NOTIFICATION_SERVICE)方法來獲取,方法以下。

[java] view plain copy 在CODE上查看代碼片派生到個人代碼片
  1. NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  


使用步驟:


流程模塊:

第一步:

建立一個通知欄的Builder構造類  (Create a Notification Builder)

第二步:

定義通知欄的Action  (Define the Notification's Action)

第三步:

設置通知欄點擊事件    (Set the Notification's Click Behavior)

第四步:

通知   (Issue the Notification)


代碼模塊:


實現系統默認的通知欄效果:

第一步:獲取狀態通知欄管理:

 
  1. NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  


第二步:實例化通知欄構造器NotificationCompat.Builder:

 
  1. NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);  


第三步:對Builder進行配置:

 

 
  1. mBuilder.setContentTitle("測試標題")//設置通知欄標題  
  2.     .setContentText("測試內容") /<span style="font-family: Arial;">/設置通知欄顯示內容</span>  
  3.     .setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL)) //設置通知欄點擊意圖  
  4. //  .setNumber(number) //設置通知集合的數量  
  5.     .setTicker("測試通知來啦") //通知首次出如今通知欄,帶上升動畫效果的  
  6.     .setWhen(System.currentTimeMillis())//通知產生的時間,會在通知信息裏顯示,通常是系統獲取到的時間  
  7.     .setPriority(Notification.PRIORITY_DEFAULT) //設置該通知優先級  
  8. //  .setAutoCancel(true)//設置這個標誌當用戶單擊面板就可讓通知將自動取消    
  9.     .setOngoing(false)//ture,設置他爲一個正在進行的通知。他們一般是用來表示一個後臺任務,用戶積極參與(如播放音樂)或以某種方式正在等待,所以佔用設備(如一個文件下載,同步操做,主動網絡鏈接)  
  10.     .setDefaults(Notification.DEFAULT_VIBRATE)//向通知添加聲音、閃燈和振動效果的最簡單、最一致的方式是使用當前的用戶默認設置,使用defaults屬性,能夠組合  
  11.     //Notification.DEFAULT_ALL  Notification.DEFAULT_SOUND 添加聲音 // requires VIBRATE permission  
  12.     .setSmallIcon(R.drawable.ic_launcher);//設置通知小ICON  


對應的各個方法的屬性(部分方法以上代碼中已經做註釋,就再也不介紹):

(1)方法:設置提醒標誌符Flags

功能:提醒標誌符,向通知添加聲音、閃燈和振動效果等設置達到通知提醒效果,能夠組合多個屬性

有2種設置方法:

1.實例化通知欄以後經過給他添加.flags屬性賦值。

  1. Notification notification = mBuilder.build();  
  2. notification.flags = Notification.FLAG_AUTO_CANCEL;  

2.經過setContentIntent(PendingIntent intent)方法中的意圖設置對應的flags

  1. public PendingIntent getDefalutIntent(int flags){  
  2.     PendingIntent pendingIntent= PendingIntent.getActivity(this, 1, new Intent(), flags);  
  3.     return pendingIntent;  
  4. }  

提醒標誌符成員:

Notification.FLAG_SHOW_LIGHTS              //三色燈提醒,在使用三色燈提醒時候必須加該標誌符

Notification.FLAG_ONGOING_EVENT          //發起正在運行事件(活動中)

Notification.FLAG_INSISTENT   //讓聲音、振動無限循環,直到用戶響應 (取消或者打開)

Notification.FLAG_ONLY_ALERT_ONCE  //發起Notification後,鈴聲和震動均只執行一次

Notification.FLAG_AUTO_CANCEL      //用戶單擊通知後自動消失

Notification.FLAG_NO_CLEAR          //只有所有清除時,Notification纔會清除 ,不清楚該通知(QQ的通知沒法清除,就是用的這個)

Notification.FLAG_FOREGROUND_SERVICE    //表示正在運行的服務


(2)方法:.setDefaults(int defaults)     (NotificationCompat.Builder中的方法,用於提示)


功能:向通知添加聲音、閃燈和振動效果的最簡單、使用默認(defaults)屬性,能夠組合多個屬性(和方法1中提示效果同樣的)

對應屬性:

Notification.DEFAULT_VIBRATE    //添加默認震動提醒  須要 VIBRATE permission

Notification.DEFAULT_SOUND    // 添加默認聲音提醒

Notification.DEFAULT_LIGHTS// 添加默認三色燈提醒

Notification.DEFAULT_ALL// 添加默認以上3種所有提醒


(3)方法:setVibrate(long[] pattern)


功能:設置震動方式。

使用:

 

  1. .setVibrate(new long[] {0,300,500,700});  

實現效果:延遲0ms,而後振動300ms,在延遲500ms,接着在振動700ms。

以上方法的還有種寫法是

  1. mBuilder.build().vibrate = new long[] {0,300,500,700};  

以此類推,2種寫法均可以。

若是但願設置默認振動方式,設置了方法(2)中默認爲DEFAULT_VIBRATE 便可。


(4)方法:.setLights(intledARGB ,intledOnMS ,intledOffMS )


功能:android支持三色燈提醒,這個方法就是設置不一樣場景下的不一樣顏色的燈。

描述:其中ledARGB 表示燈光顏色、 ledOnMS 亮持續時間、ledOffMS 暗的時間。

注意:1)只有在設置了標誌符Flags爲Notification.FLAG_SHOW_LIGHTS的時候,才支持三色燈提醒。

 

          2)這邊的顏色跟設備有關,不是全部的顏色均可以,要看具體設備。


使用:

 

  1. .setLights(0xff0000ff, 300, 0)  

同理,如下方法也能夠設置一樣效果:

 

  1. Notification notify = mBuilder.build();  
  2. notify.flags = Notification.FLAG_SHOW_LIGHTS;  
  3. notify.ledARGB = 0xff0000ff;  
  4. notify.ledOnMS = 300;  
  5. notify.ledOffMS = 300;  

若是但願使用默認的三色燈提醒,設置了方法(2)中默認爲DEFAULT_LIGHTS便可。

(5)方法:.setSound(Uri sound)


功能:設置默認或則自定義的鈴聲,來提醒。

 

  1. //獲取默認鈴聲  
  2. .setDefaults(Notification.DEFAULT_SOUND)  
  3. //獲取自定義鈴聲  
  4. .setSound(Uri.parse("file:///sdcard/xx/xx.mp3"))  
  5. //獲取Android多媒體庫內的鈴聲  
  6. .setSound(Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "5"))  

同理相同效果的另外一種設置方法這邊就不講, 和上面的都是同樣的。


(6)方法:.setPriority(int pri)


功能:設置優先級

對應優先級描述以下圖:

 

優先級 用戶
MAX 重要而緊急的通知,通知用戶這個事件是時間上緊迫的或者須要當即處理的。
HIGH 高優先級用於重要的通訊內容,例如短消息或者聊天,這些都是對用戶來講比較有興趣的。
DEFAULT 默認優先級用於沒有特殊優先級分類的通知。
LOW 低優先級能夠通知用戶但又不是很緊急的事件。
MIN 用於後臺消息 (例如天氣或者位置信息)。最低優先級通知將只在狀態欄顯示圖標,只有用戶下拉通知抽屜才能看到內容。

對應屬性(做用看上圖就可知道):

 

Notification.PRIORITY_DEFAULT

Notification.PRIORITY_HIGH

Notification.PRIORITY_LOW

Notification.PRIORITY_MAX

Notification.PRIORITY_MIN


 

(7)方法:setOngoing(boolean ongoing)


功能:設置爲ture,表示它爲一個正在進行的通知。他們一般是用來表示一個後臺任務,用戶積極參與(如播放音樂)或以某種方式正在等待,所以佔用設備(如一個文件下載,同步操做,主動網絡鏈接)


(8)方法:setProgress(int max, int progress,boolean indeterminate)


屬性:max:進度條最大數值  、progress:當前進度、indeterminate:表示進度是否不肯定,true爲不肯定,以下第3幅圖所示  ,false爲肯定下第1幅圖所示

功能:設置帶進度條的通知,能夠在下載中使用

效果圖以下:

       注意:此方法在4.0及之後版本纔有用,若是爲使用:若是爲肯定的進度條:調用setProgress(max, progress, false)來設置通知,在更新進度的時候在此發起通知更新progress,而且在下載完成後要移除進度條,經過調用setProgress(0, 0, false)既可。

操做結束時,調用setProgress(0, 0, false)並更新通知以移除指示條



第四步:設置通知欄PendingIntent(點擊動做事件等都包含在這裏)

在第三步中,沒有提到一個方法,就是setContentIntent(PendingIntent intent)這個方法,這裏拿到這裏講。



 

知識點

1)什麼是PendingIntent


PendingIntent和Intent略有不一樣,它能夠設置執行次數,主要用於遠程服務通訊、鬧鈴、通知、啓動器、短信中,在通常狀況下用的比較少。


2)PendingIntent什麼用


Notification支持多種Intent來響應單擊事件、消除事件、處理緊急狀態的全屏事件等。

這裏就用到了setContentIntent(PendingIntent intent)來處理以上這麼多的事件。


3)相關屬性和方法

屬性:

PendingIntent的位標識符:

FLAG_ONE_SHOT   表示返回的PendingIntent僅能執行一次,執行完後自動取消

FLAG_NO_CREATE     表示若是描述的PendingIntent不存在,並不建立相應的PendingIntent,而是返回NULL

FLAG_CANCEL_CURRENT      表示相應的PendingIntent已經存在,則取消前者,而後建立新的PendingIntent,這個有利於數據保持爲最新的,能夠用於即時通訊的通訊場景

FLAG_UPDATE_CURRENT     表示更新的PendingIntent


方法:


能夠看出,它支持多種相應方式,有Activity、Broadcast、Service,就根據你自身需求去選擇。


在各類狀況下狀況下它還會根據各類狀況出發效果:

 

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

deleteIntent:當用戶點擊所有清除按鈕時,響應該清除事件的Intent;

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


例如:在執行了點擊通知以後要跳轉到指定的XXX的Activity的時候,能夠設置如下方法來相應點擊事件:

  1. Intent intent = new Intent(context,XXX.class);  
  2. PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);  
  3. mBuilder.setContentIntent(pendingIntent)  


例如:在執行了清空所有的通知操做時候,能夠設置如下方法來相應這個事件:

採用setDeleteIntent(PendingIntent intent)方法或按照如下寫法

  1. Intent deleteIntent = new Intent();  
  2. deleteIntent.setClass(context, XXXReceiver.class);  
  3. deleteIntent.setAction(DELETE_ACTION);  
  4. notification.deleteIntent = PendingIntent.getBroadcast(context, 0, deleteIntent, 0);  


例如:在響應緊急事件(如來電)時候,能夠設置如下方法來相應這個事件:

採用setFullScreenIntent(PendingIntent intent, boolean highPriority)


第五步,最簡單的一部,發送通知請求

 

  1. mNotificationManager.notify(notifyId, mBuilder.build());  或者mNotificationManager.notify(notifyId, mBuilder.getNotification());



拓展


實現自定義的通知欄效果:

這裏要用到RemoteViews這個類。實現如下2種自定義佈局。


注意:

Notification的自定義佈局是RemoteViews,和其餘RemoteViews同樣,在自定義視圖佈局文件中,僅 支持FrameLayout、LinearLayout、RelativeLayout三種佈局控件和AnalogClock、Chronometer、 Button、ImageButton、ImageView、ProgressBar、TextView、ViewFlipper、ListView、 GridView、StackView和AdapterViewFlipper這些顯示控件,不支持這些類的子類或Android提供的其餘控件。不然會 引發ClassNotFoundException異常

 


步驟以下:

1)建立自定義視圖

2)獲取遠程視圖對象(注:Notification的contentView不能爲空)

3)設置PendingIntent(來響應各類事件)

4)發起Notification

大致4步驟這裏就不詳細說了,下面就把DEMO中的列子拿出來講下


樣式:

1.自定義帶按鈕通知欄(以下樣式)

 

「正在進行的」通知使用戶瞭解正在運行的後臺進程。例如,音樂播放器能夠顯示正在播放的音樂。也能夠用來顯示須要長時間處理的操做,例以下載或編碼視頻。「正在進行的」通知不能被手動刪除。

 

 




實現方法以下:
 
  1. /** 
  2.      * 帶按鈕的通知欄 
  3.      */  
  4.     public void showButtonNotify(){  
  5.         NotificationCompat.Builder mBuilder = new Builder(this);  
  6.         RemoteViews mRemoteViews = new RemoteViews(getPackageName(), R.layout.view_custom_button);  
  7.         mRemoteViews.setImageViewResource(R.id.custom_song_icon, R.drawable.sing_icon);  
  8.         //API3.0 以上的時候顯示按鈕,不然消失  
  9.         mRemoteViews.setTextViewText(R.id.tv_custom_song_singer, "周杰倫");  
  10.         mRemoteViews.setTextViewText(R.id.tv_custom_song_name, "七里香");  
  11.         //若是版本號低於(3。0),那麼不顯示按鈕  
  12.         if(BaseTools.getSystemVersion() <= 9){  
  13.             mRemoteViews.setViewVisibility(R.id.ll_custom_button, View.GONE);  
  14.         }else{  
  15.             mRemoteViews.setViewVisibility(R.id.ll_custom_button, View.VISIBLE);  
  16.         }  
  17.         //  
  18.         if(isPlay){  
  19.             mRemoteViews.setImageViewResource(R.id.btn_custom_play, R.drawable.btn_pause);  
  20.         }else{  
  21.             mRemoteViews.setImageViewResource(R.id.btn_custom_play, R.drawable.btn_play);  
  22.         }  
  23.         //點擊的事件處理  
  24.         Intent buttonIntent = new Intent(ACTION_BUTTON);  
  25.         /* 上一首按鈕 */  
  26.         buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_PREV_ID);  
  27.         //這裏加了廣播,所及INTENT的必須用getBroadcast方法  
  28.         PendingIntent intent_prev = PendingIntent.getBroadcast(this, 1, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT);  
  29.         mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_prev, intent_prev);  
  30.         /* 播放/暫停  按鈕 */  
  31.         buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_PALY_ID);  
  32.         PendingIntent intent_paly = PendingIntent.getBroadcast(this, 2, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT);  
  33.         mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_play, intent_paly);  
  34.         /* 下一首 按鈕  */  
  35.         buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_NEXT_ID);  
  36.         PendingIntent intent_next = PendingIntent.getBroadcast(this, 3, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT);  
  37.         mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_next, intent_next);  
  38.           
  39.         mBuilder.setContent(mRemoteViews)  
  40.                 .setContentIntent(getDefalutIntent(Notification.FLAG_ONGOING_EVENT))  
  41.                 .setWhen(System.currentTimeMillis())// 通知產生的時間,會在通知信息裏顯示  
  42.                 .setTicker("正在播放")  
  43.                 .setPriority(Notification.PRIORITY_DEFAULT)// 設置該通知優先級  
  44.                 .setOngoing(true)  
  45.                 .setSmallIcon(R.drawable.sing_icon);  
  46.         Notification notify = mBuilder.build();  
  47.         notify.flags = Notification.FLAG_ONGOING_EVENT;  
  48.         mNotificationManager.notify(notifyId, notify);  
  49.     }  
注意:帶按鈕的佈局相應點擊事件在3.0如下版本沒有用,因此這邊做了系統版本判斷,來顯示消失按鈕。


2.自定義不帶按鈕通知欄


實現方法以下:
 
  1.         //先設定RemoteViews  
  2.         RemoteViews view_custom = new RemoteViews(getPackageName(), R.layout.view_custom);  
  3.         //設置對應IMAGEVIEW的ID的資源圖片  
  4.         view_custom.setImageViewResource(R.id.custom_icon, R.drawable.icon);  
  5. //      view_custom.setInt(R.id.custom_icon,"setBackgroundResource",R.drawable.icon);  
  6.         view_custom.setTextViewText(R.id.tv_custom_title, "今日頭條");  
  7.         view_custom.setTextViewText(R.id.tv_custom_content, "金州勇士官方宣佈球隊已經解僱了主帥馬克-傑克遜,隨後宣佈了最後的結果。");  


以後調用:
  1. <span style="font-family: Arial, Helvetica, sans-serif;">mBuilder.setContent(view_custom)</span>  
來設定自定義的這個佈局。



實現:大視圖風格通知(注: 只在通知被展開時顯示

 

什麼時候展開:通知處在頂端,或者用戶經過收拾展開

收件箱風格的通知:

 

相比普通視圖,只多出:7. 詳情區域


效果圖以下:


詳情區域根據用途可有多種風格:

1.NotificationCompat.BigPictureStyle 大圖片風格:詳情區域包含一個256dp高度的位圖

2.NotificationCompat.BigTextStyle 大文字風格:顯示一個大的文字塊

3.NotificationCompat.InboxStyle  收件箱風格:顯示多行文字  


各類風格都具備如下常規視圖不具備的內容選項:

1.大標題:在展開視圖時替代普通視圖的標記

2.總結文字:容許你在詳情區域之下增長一行內容


拿收件箱風格爲例,實現代碼以下:
  1.         NotificationCompat.BigPictureStyle inboxStyle = new NotificationCompat.InboxStyle();  
  2.         String[] events = new String[5];  
  3.         // Sets a title for the Inbox style big view  
  4.         inboxStyle.setBigContentTitle("大視圖內容:");  
  5.         // Moves events into the big view  
  6.         for (int i=0; i < events.length; i++) {  
  7.             inboxStyle.addLine(events[i]);  
  8.         }  
  9.         mBuilder.setContentTitle("測試標題")  
  10.                 .setContentText("測試內容")  
  11. //              .setNumber(number)//顯示數量  
  12.                 .setStyle(inboxStyle)//設置風格  
  13.                 .setTicker("測試通知來啦");  



開發中碰到的問題

注:下面所指的低版本是指2.3及2.3如下版本)

1.如何取消掉通知欄上的通知

  (1)設置對應的flags,讓用戶點擊既被消除:

notification.flags = FLAG_AUTO_CANCEL;

    (2) 經過手動消除某項或則所有通知

mNotificationMgr.cancle(NOTIFICATION_ID);//消除對應ID的通知

mNotificationMgr.cancleAll();//消除建立的全部通知


2.低版本中的部分方法已經被棄用的

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

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

這些方法都已經被啓用,雖然還有效果,但是不建議使用。因此開發過程當中儘可能使用NotificationCompat.Builder(this)的構建方法去建立一個通知類。


3.低版本中會報的錯誤及解決方案:

解決方案:若是在高版本不會出錯,而在2.3上面報了這個錯誤,經過開發文檔中的如下知道你能夠找打:

Activity in your app, and you should always start that Activity when users click the notification. To do this, use the setContentIntent() method.

設置contentIntent,若是你點擊沒有意圖,能夠在賦值的的Intent中設置爲new Intent()既可,切記contentIntent不能爲空。

代碼以下:

 

  1. public PendingIntent getDefalutIntent(int flags){  
  2.     PendingIntent pendingIntent= PendingIntent.getActivity(this, 1, new Intent(), flags);  
  3.     return pendingIntent;  
  4. }  


(2)錯誤代 碼:android.app.RemoteServiceException: Bad notification posted from package com.example.notifications: Couldn't expand RemoteViews for: StatusBarNotification(package=com.example.notifications id=101 tag=null notification=Notification(vibrate=null,sound=null,defaults=0x0,flags=0x2))

解決方法:


4.低版本中,自定義的通知欄中若是帶有按鈕,可能按鈕點擊事件會失靈

解決方法:看其它的應用,好像在低版本都會隱藏掉那些按鈕,就是爲了避免影響用戶體驗,因此應該就這麼解決,判斷版本號在去決定是否如今按鈕。


5.低版本中,自定義佈局中的字體顏色看不清

如右圖:

解決方案:

因爲2.3及以前版本,背景設是白色的那咱們定義字體顏色爲系統預設的顏色:

?android:attr/textColorPrimary

在資源的src/values目錄中的style.xml文件中設置它標題和內容的樣式爲:

  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <resources>    
  3.     
  4.     <style name="NotificationContent">    
  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>  


在2.3以後的版本中(即API >=9的版本中),在資源文件下的src/values-v9目錄中的style.xml文件中設置它標題和內容的樣式爲:

  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <resources>    
  3.     
  4.     <style name="NotificationContent" parent="android:TextAppearance.StatusBar.EventContent" />    
  5.     
  6.     <style name="NotificationTitle" parent="android:TextAppearance.StatusBar.EventContent.Title" />    
  7.     
  8. </resources>    


最後賦給自定義佈局中的對應標題和內容對應的style便可。

對應解決網址:

1.http://stackoverflow.com/questions/6250356/how-to-use-default-notification-style

2.http://stackoverflow.com/questions/4867338/custom-notification-layouts-and-text-colors/7320604#7320604

3.http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomExpandedView   (官方文檔)

http://developer.android.com/about/versions/android-2.2-highlights.html


6.低版本中mBuilder.setProgress(100, progress, false);沒用,不顯示進度條

解決方法:此方法在4.0及之後版本纔有用,若是爲早期版本:須要自定義通知佈局,其中包含ProgressBar視圖



7.自定義佈局的時候,不一樣版本方法不同。(弄了半天,在2.3版本不顯示,原來是方法不兼容)


2.3及2.3以前:

經過

  1. Notification notify = mBuilder.build();  
  2. notify.contentView = view_custom;  
  3. mNotificationManager.notify(notifyId, notify)  

方法賦予VIEW。

2.3以後:

經過Builder如下方法賦於自定義佈局。

mBuilder.setContent(view_custom)



這裏就不貼DEMO中的代碼了,你們能夠下個DEMO本身看,裏面也都有註釋的,可能有的地方會有錯誤,忘你們指出,以便及時修改,謝謝。


一個DEMO讓你更懂Notification

DEMO截圖:

          


DEMO下載:下載地址

相關文章
相關標籤/搜索