用最新版的SDK,在做狀態欄通知時,使用了Notification的setLatestEventInfo(),結果提示:
The method setLatestEventInfo(Context, CharSequence, CharSequence, PendingIntent) is undefined for the type Notification
搞了半天,原來原因是SDK中不存在setLatestEventInfo()這個函數方法,所以出錯了。
在sdk\sources\android-23\android\app\Notification.java這個文件中,setLatestEventInfo()是@removed,即在安卓6.0開始的SDK中,它不存在了。
而sdk\sources\android-22\android\app\Notification.java這個文件中,setLatestEventInfo()仍存在,只是@deprecated Use {@link Builder} instead.
國外網上找到說:把當前這個項目的屬性的project build target由android 6.0改成android 5.1.1。
OK,問題解決。高於API Level 11,可使用Notification.Builder,但我需要支持API 8
Notification即通知,用於在通知欄顯示提示信息。
在較新的版本中(API level > 11),Notification類中的一些方法被Android聲明deprecated(棄用),其實基本上相當於全部棄用了,因為這個類本身方法就少得可憐。
Android官方聲明棄用,一定有它的理由,雖然我也不知道是什么。奈何本人輕度強迫症患者,人家都建議你不要用了,那就不要老是恪守着N年前的東西了。
就像是以前,一說到標簽頁,大家基本上都會想到TabHost配合ActivityGroup,但Android后來提倡Fragment。
廢話說多了,還是小結一下使用方法。下面按照創建一個通知的步驟一步一步來,同時給出新舊實現方法。
1、獲取Notification管理器
NotificationManager noteMng = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
2、新建一個Notification,設置狀態欄顯示樣式
private Notification note;
//API 11以下
note = new Notification(R.drawable.ico_launcher "顯示於屏幕頂端狀態欄的文本", System.currentTimeMillis());
//API 11及以上
Notification.Builder builder = new Notification.Builder(nowContext).setTicker("顯示於屏幕頂端狀態欄的文本")
.setSmallIcon(R.drawable.ic_laucher);
API 11以上版本中,狀態欄顯示的樣式跟下拉通知欄中顯示的樣式,可以一起設置,就是通過Notification.Builder類來實現,這里的Builder只調用了兩個方法來設置狀態欄顯示樣式。
3、設置Notification標志位(非必要步驟)
//FLAG_ONGOING_EVENT表明有程序在運行,該Notification不可由用戶清除
note.flags = Notification.FLAG_ONGOING_EVENT;
4、設置點擊Notification后的觸發事件
//通過Intent,使得點擊Notification之后會啟動新的Activity
Intent i = new Intent(nowContext, AnotherActivity.class);
//該標志位表示如果Intent要啟動的Activity在棧頂,則無須創建新的實例
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
pendingIntent = PendingIntent.getActivity(nowContext, 100, i, PendingIntent.FLAG_UPDATE_CURRENT);
5、設置Notification在通知欄里的樣式
(1)系統默認樣式
//API 11以下:
note.setLatestEventInfo(nowContext, "take me to your heart", "Micheal learn to rock", pendingIntent);
//API 16及以上,build()方法要求API 16及以上
//一會API 11以上,一會API16以上,我也很想知道Android的API是怎么設計的
note = builder.setContentIntent(pendingIntent).setContentTitle("title").setContentText("text").build();
(2)自定義樣式:
自定義樣式,就是讓Notification在通知欄顯示成自定義的xml布局
應當注意的是,Notification的自定義樣式,只支持以下可視組件:
FrameLayout, LinearLayout, RelativeLayout
TextView, Button, AnalogClock, ImageView, ImageButton, Chronometer, ProgressBar
RemoteView view = new RemoteView(nowActivity.getPackageName(), R.layout.note_layout);
//API 11以下
note.contentView = view;
note.contentIntent = pendingIntent;
//API 16及以上,又是build()方法導致的,汗。。
note = builder.setContent(view).setContentIntent(pendingIntent).build();
這個步驟里有一個很值得注意的地方:pendingIntent被設置為note的contentIntent的值,就意味着點擊了這個通知才會觸發該Intent。
那么如果只是想讓自定義布局里的某個按鈕觸發呢?比如說,弄了一個音樂播放器,Service負責播放音樂,Notification顯示當前播放進度和一些簡單的暫停按鈕、上一首、下一首按鈕,讓用戶不用再打開界面就可以通過Notification上的按鈕操縱音樂播放。
假設說想讓自定義布局里的一個id為R.id.button1的按鈕來觸發這個Intent,可以如下操作:
view.setOnClickPendingIntent(R.id.button1, pendingIntent);//在上面創建RemoteView實例后加上這句
然后注意,pendingIntent已經綁定到按鈕上了,上面Notificatiion實例中,設置contentIntent的語句要去掉。
6、發布該通知,第一個參數為該notification的ID
noteMng.notify(10, note);