最近一直在研究 android ,并一邊研究一邊做應用。其中遇到了把程序通知常駐在 Notification 欄,并且不能被 clear 掉(就像android QQ一樣)的問題。經過研究實現了其功能,現把 Notification 的使用總結如下:?
Notification 的使用需要導入 3 個類
1 2 3 | import ?android.app.PendingIntent; import ?android.app.NotificationManager; import ?android.app.Notification; |
代碼示例及說明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);?????????????? Notification n = new ?Notification(R.drawable.chat, "Hello,there!" , System.currentTimeMillis());???????????? n.flags = Notification.FLAG_AUTO_CANCEL;??????????????? Intent i = new ?Intent(arg0.getContext(), NotificationShow. class ); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);?????????? //PendingIntent PendingIntent contentIntent = PendingIntent.getActivity( ???????? arg0.getContext(), ???????? R.string.app_name, ???????? i, ???????? PendingIntent.FLAG_UPDATE_CURRENT); ???????????????? ?n.setLatestEventInfo( ???????? arg0.getContext(), ???????? "Hello,there!" , ???????? "Hello,there,I'm john." , ???????? contentIntent); nm.notify(R.string.app_name, n); |
下面依次對每一段代碼進行分析:
1 | NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); |
創建 NotificationManager,其中創建的 nm 對象負責“發出”與“取消”? Notification。
1 2 | Notification n = new ?Notification(R.drawable.chat, "Hello,there!" , System.currentTimeMillis());???????????? n.flags = Notification.FLAG_ONGOING_EVENT;? |
android10開發者選項。創建 Notification ,參數依次為:icon的資源id,在狀態欄上展示的滾動信息,時間。其中創建的 n 對象用來描述出現在系統通知欄的信息,之后我們將會看到會在 n 對象上設置點擊此條通知發出的Intent。
1 | n.flags = Notification.FLAG_AUTO_CANCEL; |
設置 n.flags 為 Notification.FLAG_AUTO_CANCEL ,該標志表示當用戶點擊 Clear 之后,能夠清除該通知。
1 2 | Intent i = new ?Intent(arg0.getContext(), NotificationShow. class ); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK); |
創建一個Intent,該Intent使得當用戶點擊該通知后發出這個Intent
請注意,如果要以該Intent啟動一個Activity,一定要設置 Intent.FLAG_ACTIVITY_NEW_TASK 標記。
Intent.FLAG_ACTIVITY_CLEAR_TOP :如果在當前Task中,有要啟動的Activity,那么把該Acitivity之前的所有Activity都關掉,并把此Activity置前以避免創建Activity的實例
Intent.FLAG_ACTIVITY_NEW_TASK :系統會檢查當前所有已創建的Task中是否有該要啟動的Activity的Task,若有,則在該Task上創建Activity,若沒有則新建具有該Activity屬性的Task,并在該新建的Task上創建Activity。更多請參見 “?(轉載)Android下Affinities和Task?”
1 2 3 4 5 6 | //PendingIntent PendingIntent contentIntent = PendingIntent.getActivity( ???????? arg0.getContext(), ???????? R.string.app_name, ???????? i, ???????? PendingIntent.FLAG_UPDATE_CURRENT); |
android studio運行。PendingIntent 為Intent的包裝,這里是啟動Intent的描述,PendingIntent.getActivity 返回的PendingIntent表示,此PendingIntent實例中的Intent是用于啟動 Activity 的Intent。PendingIntent.getActivity的參數依次為:Context,發送者的請求碼(可以填0),用于系統發送的Intent,標志位。
其中 PendingIntent.FLAG_UPDATE_CURRENT? 表示如果該描述的PendingIntent已存在,則改變已存在的PendingIntent的Extra數據為新的PendingIntent的Extra數據。
這里再簡要說一下 Intent 與 PendingIntent 的區別:
Intent :意圖,即告訴系統我要干什么,然后系統根據這個Intent做對應的事。如startActivity相當于發送消息,而Intent是消息的內容。
PendingIntent :包裝Intent,Intent 是我們直接使用 startActivity , startService 或 sendBroadcast 啟動某項工作的意圖。而某些時候,我們并不能直接調用startActivity , startServide 或 sendBroadcast ,而是當程序或系統達到某一條件才發送Intent。如這里的Notification,當用戶點擊Notification之后,由系統發出一條Activity 的 Intent 。因此如果我們不用某種方法來告訴系統的話,系統是不知道是使用 startActivity ,startService 還是 sendBroadcast 來啟動Intent 的(當然還有其他的“描述”),因此這里便需要PendingIntent。
1 2 3 4 5 | n.setLatestEventInfo( ???????? arg0.getContext(), ???????? "Hello,there!" , ???????? "Hello,there,I'm john." , ???????? contentIntent); |
設置顯示在通知下拉框中的信息,參數依次為:Context,標題,內容,PendingIntent。
1 | nm.notify(R.string.app_name, n); |
android retrofit?啟動Notification,參數依次為:在你的程序中標識Notification的id值(用來區分同一程序中的不同Notifycation,如果程序中只有一個Notification那么這里隨便你填什么都可以,不過類型必須要為int),要通知的Notification。
如何使自己的Notification像Android QQ一樣能出現在 “正在運行的”欄目下面
其實很簡單,只需設置Notification.flags = Notification.FLAG_ONGOING_EVENT;便可以了。
如何改變 Notification 在“正在運行的”欄目下面的布局
創建 RemoteViews 并賦給 Notification.contentView ,再把 PendingIntent 賦給 Notification.contentIntent 便可以了,如:
1 2 3 4 5 6 7 8 9 10 11 12 13 | PendingIntent contentIntent = PendingIntent.getActivity( ???? arg0.getContext(), ???? R.string.app_name, ???? i, ???? PendingIntent.FLAG_UPDATE_CURRENT); ???????????? ?RemoteViews rv = new ?RemoteViews(Main. this .getPackageName(), R.layout.notification_view); rv.setImageViewResource(R.id.image, R.drawable.chat); rv.setTextViewText(R.id.text, "Hello,there,I'm john." ); n.contentView = rv; n.contentIntent = contentIntent; nm.notify(R.string.app_name, n); |
注意,如果使用了contentView,那么便不要使用Notification.setLatestEventInfo。如果setLatestEventInfo在賦給 Notification.contentView 的代碼之后,那么contentView的效果將被覆蓋,顯示的便是 setLatestEventInfo 的效果;如果 setLatestEventInfo 在 Notification.contentView 的代碼之前,那么顯示的便是 Notification.contentView 的效果,也就是說不管你想要setLatestEventInfo 或 contentView 的自定義效果,請保證始終只有一句設置代碼,因為在最后一句綁定的時候,之前的設置contentView或setLatestEventInfo的代碼都是完全沒有必要的。