Android的通知事件,可以在狀態列上面顯示,滾動顯示通知突然內容以及表徵圖,可以設定在狀態列展開的時候,顯示通知項的標題和內容,以及點擊它所開啟的應用程式。。。
NotificationManager,通過getSystemService(Context.NOTIFICATION_SERVICE);來擷取系統的通知管理器
建立一個通知事件,Notification notification = new Notification(R.drawable.icon, “狀態列上滾動顯示的標題”, System.currentTimeMillis());
其中第一參數為notification在狀態列上顯示的表徵圖,第二個為添加時狀態列上顯示的標題,最後為通知事件添加的時間。
通過notification.flags參數設定通知事件的標誌,如是屬於“進行中的”還是“通知”等,具體得看reference了,,,
notification.flags |= Notification.FLAG_ONGOING_EVENT;
設定狀態列展開時,通知事件的標題和內容,以及點擊所開啟的程式。
Intent intent = new Intent(mContext, NotificationCancelAll.class);
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
notification.setLatestEventInfo(mContext, "NotificationTitle",
"Notification Content!", pendingIntent);
如果有需要可以,自己定義在狀態展開時所顯示的view
RemoteViews remoteView = new RemoteViews(getPackageName(), R.layout.notification_test);
notification.contentView = remoteView;
最後添加設定好的notification即可,其中R.string.app_name為這個通知事件的ID,為後面移除這個通知用的。
mNotifyManager.notify(R.string.app_name, notification);
移除:
notifyManager.cancel(R.string.app_name);
/**<br /> * Android添加Notification的代碼片斷,防止忘記了<br /> * Yao.GUET<br /> * blog: http://blog.csdn.net/Yao_GUET<br /> */<br />public void addNotify() {<br />// TODO Auto-generated method stub<br />NotificationManager mNotifyManager = (NotificationManager)getSystemService(<br />Context.NOTIFICATION_SERVICE);<br />// public Notification (int icon, CharSequence tickerText, long when)<br />// icon will show on status bar<br />// tickerText is the display content on status bar.<br />Notification notification = new Notification(R.drawable.icon,<br />null, System.currentTimeMillis());</p><p>// set the notification defaults,<br />// DEFAULT_ALL=DEFAULT_VIBRATE | DEFAULT_LIGHTS | DEFAULT_SOUND<br />notification.defaults = Notification.DEFAULT_ALL;</p><p>// set the notification flags<br />// FLAG_ONGOING_EVENT means notification will disaplay under ongoing<br />// other will display under Notifications<br />notification.flags |= Notification.FLAG_ONGOING_EVENT;</p><p>// after a 100ms delay, vibrate for 250ms, pause for 100 ms and<br /> // then vibrate for 500ms.<br /> // notification.vibrate = new long[] { 100, 250, 100, 500};</p><p>// set the intent, it will open when click the notification on expended status bar<br />// intent.putExtra("img", R.drawable.icon);<br />Intent intent = new Intent(mContext, NotificationCancelAll.class);<br />PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);</p><p>// set the title and content show in notification panel<br />// pendingIntent is onClick event<br />notification.setLatestEventInfo(mContext, "NotificationTitle",<br />"Notification Content!", pendingIntent);</p><p> // custom the notification view in expended status bar<br /> // RemoteViews remoteView = new RemoteViews(getPackageName(), R.layout.notification_test);<br /> // notification.contentView = remoteView;</p><p>// add the notification<br />// here use R.string.app_name as the notification id,<br />// cause it is unique integer in your app, meanwhile it could be any int you want.<br />mNotifyManager.notify(R.string.app_name, notification);<br />}