最近一直在研究 android ,並一邊研究一邊作應用。其中遇到了把程序通知常駐在 Notification 欄,而且不能被 clear 掉(就像android QQ同樣)的問題。通過研究實現了其功能,現把 Notification 的使用總結以下:
html
Notification 的使用須要導入 3 個類java
1
2
3
|
import
android.app.PendingIntent;
import
android.app.NotificationManager;
import
android.app.Notification;
|
代碼示例及說明android
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);
|
下面依次對每一段代碼進行分析:app
1
|
NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
|
建立 NotificationManager,其中建立的 nm 對象負責「發出」與「取消」 Notification。ide
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 以後,可以清除該通知。this
1
2
|
Intent i =
new
Intent(arg0.getContext(), NotificationShow.
class
);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
|
建立一個Intent,該Intent使得當用戶點擊該通知後發出這個Intentspa
請注意,若是要以該Intent啓動一個Activity,必定要設置 Intent.FLAG_ACTIVITY_NEW_TASK 標記。code
Intent.FLAG_ACTIVITY_CLEAR_TOP :若是在當前Task中,有要啓動的Activity,那麼把該Acitivity以前的全部Activity都關掉,並把此Activity置前以免建立Activity的實例htm
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的代碼都是徹底沒有必要的。
shougao result(source code):
public class NotifytestActivity extends Activity {
private NotificationManager myNotificationManager;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Intent notifyIntent = new Intent(this, Notifynew.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent appIntent = PendingIntent.getActivity(this, 0, notifyIntent, 0);
Notification myNotification = new Notification();
myNotification.icon = R.drawable.ic_launcher;
myNotification.tickerText = "tickerText";
myNotification.defaults = Notification.DEFAULT_SOUND;
myNotification.setLatestEventInfo(this, "contenttitle", "contenttext", appIntent);
myNotificationManager.notify(0, myNotification);
}
public class Notifynew extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); Toast.makeText(this, "new info", Toast.LENGTH_SHORT).show(); // finish(); }