用過Android版的QQ的都知道,QQ返回的時候在頂部通知裏會出現一個企鵝,表明QQ正在運行,可以拉開通知,點擊手機QQ出現主界面,繼續運行。
所以我想讓自己的軟件也出現這麼一個通知,並且可以快速的打開查看。
Android應用開發詳解 8.3 Notification和NotificationManager的使用 P178
/**
* 添加頂部通知
* @author liuzhao
*/
public void AddNotification(){
//添加通知到頂部任務欄
//獲得NotificationManager實例
String service
= NOTIFICATION_SERVICE;
nm = (NotificationManager)getSystemService(service);
//實例化Notification
n = new
Notification();
//設置顯示圖標
int icon
= R.drawable.ic_launcher_home;
//設置提示信息
String tickerText
= “我的程序”;
//顯示時間
long when
= System.currentTimeMillis();
n.icon
= icon;
n.tickerText
= tickerText;
n.when
= when;
//顯示在“正在進行中”
n.flags
= Notification.FLAG_ONGOING_EVENT;
//實例化Intent
Intent intent
= new
Intent(tykmAndroid.this,tykmAndroid.class);
//獲得PendingIntent
PendingIntent pi
= PendingIntent.getActivity(tykmAndroid.this,
0,
intent, 0);
//設置事件信息,顯示在拉開的裏面
n.setLatestEventInfo(tykmAndroid.this,
“我的軟件”,
“我的軟件正在運行……”,
pi);
//發出通知
nm.notify(ID,n);
}
正在進行的和持續的Notification
通過設置FLAG_INSISTENT和FLAG_ONGOING_EVENT 標誌位可以讓Notification成爲持續或正在進行的Notification。
Notification標記爲ONGOING,如下面的代碼所示,它就能用於表示當前正在進行的事件(如來電)。正在進行的事件與“普通的”Notification區別在擴展的狀態條窗口中。
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
持續的Notification一直重複,直到用戶取消。下面的代碼給出瞭如何設置Notification爲持續的:
notification.flags = notification.flags | Notification.FLAG_INSISTENT;
持續Notification反覆重複開頭的Notification效果,直到用戶取消。持續的Notification應該保留給如鬧鐘的情形,它需要及時的採取響應。