Android學習筆記,Notification通知事件

來源:互聯網
上載者:User

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 />} 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.