在Launcher開發中需要使用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; |
創建 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); |
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); |
啓動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的代碼都是完全沒有必要的。