Android 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;
nm.flags=Notification.FLAG_NO_CLEAR;      //可以讓通知不被刪掉        
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);
nm.cancel(R.string.app_name);  //取消通知

下面依次對每一段代碼進行分析:

?
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);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM