最近一直在研究 android ,并一邊研究一邊做應(yīng)用松却。其中遇到了把程序通知常駐在 Notification 欄,并且不能被 clear 掉(就像android QQ一樣)的問(wèn)題溅话。經(jīng)過(guò)研究實(shí)現(xiàn)了其功能晓锻,現(xiàn)把 Notification 的使用總結(jié)如下:
Notification 的使用需要導(dǎo)入 3 個(gè)類
import android.app.PendingIntent;
import android.app.NotificationManager;
import android.app.Notification;
代碼示例及說(shuō)明
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);
下面依次對(duì)每一段代碼進(jìn)行分析:
NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
創(chuàng)建 NotificationManager,其中創(chuàng)建的 nm 對(duì)象負(fù)責(zé)“發(fā)出”與“取消” Notification飞几。
Notification n = new Notification(R.drawable.chat, "Hello,there!", System.currentTimeMillis());
n.flags = Notification.FLAG_ONGOING_EVENT;
創(chuàng)建 Notification 砚哆,參數(shù)依次為:icon的資源id,在狀態(tài)欄上展示的滾動(dòng)信息屑墨,時(shí)間躁锁。其中創(chuàng)建的 n 對(duì)象用來(lái)描述出現(xiàn)在系統(tǒng)通知欄的信息纷铣,之后我們將會(huì)看到會(huì)在 n 對(duì)象上設(shè)置點(diǎn)擊此條通知發(fā)出的Intent。
n.flags = Notification.FLAG_AUTO_CANCEL;
設(shè)置 n.flags 為 Notification.FLAG_AUTO_CANCEL 战转,該標(biāo)志表示當(dāng)用戶點(diǎn)擊 Clear 之后搜立,能夠清除該通知。
Intent i = new Intent(arg0.getContext(), NotificationShow.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
創(chuàng)建一個(gè)Intent槐秧,該Intent使得當(dāng)用戶點(diǎn)擊該通知后發(fā)出這個(gè)Intent
請(qǐng)注意啄踊,如果要以該Intent啟動(dòng)一個(gè)Activity,一定要設(shè)置 Intent.FLAG_ACTIVITY_NEW_TASK 標(biāo)記刁标。
Intent.FLAG_ACTIVITY_CLEAR_TOP :如果在當(dāng)前Task中颠通,有要啟動(dòng)的Activity,那么把該Acitivity之前的所有Activity都關(guān)掉命雀,并把此Activity置前以避免創(chuàng)建Activity的實(shí)例
Intent.FLAG_ACTIVITY_NEW_TASK :系統(tǒng)會(huì)檢查當(dāng)前所有已創(chuàng)建的Task中是否有該要啟動(dòng)的Activity的Task蒜哀,若有,則在該Task上創(chuàng)建Activity吏砂,若沒(méi)有則新建具有該Activity屬性的Task,并在該新建的Task上創(chuàng)建Activity乘客。
PendingIntent contentIntent = PendingIntent.getActivity(
arg0.getContext(),
R.string.app_name,
i,
PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent 為Intent的包裝狐血,這里是啟動(dòng)Intent的描述,PendingIntent.getActivity 返回的PendingIntent表示易核,此PendingIntent實(shí)例中的Intent是用于啟動(dòng) Activity 的Intent匈织。PendingIntent.getActivity的參數(shù)依次為:Context,發(fā)送者的請(qǐng)求碼(可以填0)牡直,用于系統(tǒng)發(fā)送的Intent缀匕,標(biāo)志位。
其中 PendingIntent.FLAG_UPDATE_CURRENT 表示如果該描述的PendingIntent已存在碰逸,則改變已存在的PendingIntent的Extra數(shù)據(jù)為新的PendingIntent的Extra數(shù)據(jù)乡小。
這里再簡(jiǎn)要說(shuō)一下 Intent 與 PendingIntent 的區(qū)別:
Intent :意圖,即告訴系統(tǒng)我要干什么饵史,然后系統(tǒng)根據(jù)這個(gè)Intent做對(duì)應(yīng)的事满钟。如startActivity相當(dāng)于發(fā)送消息,而Intent是消息的內(nèi)容胳喷。
PendingIntent :包裝Intent湃番,Intent 是我們直接使用 startActivity , startService 或 sendBroadcast 啟動(dòng)某項(xiàng)工作的意圖吭露。而某些時(shí)候吠撮,我們并不能直接調(diào)用startActivity , startServide 或 sendBroadcast 讲竿,而是當(dāng)程序或系統(tǒng)達(dá)到某一條件才發(fā)送Intent泥兰。如這里的Notification择浊,當(dāng)用戶點(diǎn)擊Notification之后,由系統(tǒng)發(fā)出一條Activity 的 Intent 逾条。因此如果我們不用某種方法來(lái)告訴系統(tǒng)的話琢岩,系統(tǒng)是不知道是使用 startActivity ,startService 還是 sendBroadcast 來(lái)啟動(dòng)Intent 的(當(dāng)然還有其他的“描述”)师脂,因此這里便需要PendingIntent担孔。
n.setLatestEventInfo(
arg0.getContext(),
"Hello,there!",
"Hello,there,I'm john.",
contentIntent);
設(shè)置顯示在通知下拉框中的信息吃警,參數(shù)依次為:Context糕篇,標(biāo)題,內(nèi)容酌心,PendingIntent拌消。
1
nm.notify(R.string.app_name, n);
啟動(dòng)Notification,參數(shù)依次為:在你的程序中標(biāo)識(shí)Notification的id值(用來(lái)區(qū)分同一程序中的不同Notifycation安券,如果程序中只有一個(gè)Notification那么這里隨便你填什么都可以墩崩,不過(guò)類型必須要為int),要通知的Notification侯勉。
如何使自己的Notification像Android QQ一樣能出現(xiàn)在 “正在運(yùn)行的”欄目下面
其實(shí)很簡(jiǎn)單鹦筹,只需設(shè)置Notification.flags = Notification.FLAG_ONGOING_EVENT;便可以了。
如何改變 Notification 在“正在運(yùn)行的”欄目下面的布局
創(chuàng)建 RemoteViews 并賦給 Notification.contentView 址貌,再把 PendingIntent 賦給 Notification.contentIntent 便可以了铐拐,如:
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 的效果赂摆,也就是說(shuō)不管你想要setLatestEventInfo 或 contentView 的自定義效果挟憔,請(qǐng)保證始終只有一句設(shè)置代碼,因?yàn)樵谧詈笠痪浣壎ǖ臅r(shí)候烟号,之前的設(shè)置contentView或setLatestEventInfo的代碼都是完全沒(méi)有必要的绊谭。