【起航計劃 024】2015 起航計劃 Android APIDemo的魔鬼步伐 23 App->Notification->IncomingMessage 狀態列通知

來源:互聯網
上載者:User

標籤:

應用程式可以使用Notifications來通知使用者某個事件發生了(如收到簡訊)。類NotificationManager 用來處理Notification, NotificationManager可以:

  • 在Status Bar上顯示一個新的表徵圖。
  • 在Extended status bar 視窗上顯示附加資訊或是啟動一個Activity。
  • 顯示背光/LED。
  • 使裝置震動。
  • 發出聲音等。

對於一些沒有UI的應用程式組件(如Broadcast Receiver, Services)或是非使用中的Activity,Notification是推薦使用的可以提醒使用者注意的方法。

Notification通常是在Status Bar上顯示表徵圖或是文字,此時使用者如果想瞭解Notification的詳細內容,可以按住Status Bar下拉顯示Expanded Status bar 視窗,在Expanded Status bar視窗顯示該Notification詳情並可以啟動對應的Activity。

IncomingMessage 樣本介紹了Notification的一般用法:

1. 首先是取得NotificationManager 對象:

        // look up the notification manager service        NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

 2. 然後建立Notification,建立Notification時指定顯示在Status bar的表徵圖,文字以及顯示Notification的時間:

        // construct the Notification object.        Notification notif = new Notification(R.drawable.stat_sample, tickerText,                System.currentTimeMillis());

 

 3. 然後定義當使用者開啟Extented status windows視窗時的標題及詳情。Notification常常代表了一個請求或者需要引起注意的事件,因此可以指定一個PendingIntent來響應使用者點擊這個Notification。

        // The details of our fake message        CharSequence from = "Joe";        CharSequence message;        switch ((new Random().nextInt()) % 3) {            case 0: message = "r u hungry?  i am starved"; break;            case 1: message = "im nearby u"; break;            default: message = "kthx. meet u for dinner. cul8r"; break;        }        // The PendingIntent to launch our activity if the user selects this        // notification.  Note the use of FLAG_CANCEL_CURRENT so that, if there        // is already an active matching pending intent, cancel it and replace        // it with the new array of Intents.        PendingIntent contentIntent = PendingIntent.getActivities(this, 0,                makeMessageIntentStack(this, from, message), PendingIntent.FLAG_CANCEL_CURRENT);        // The ticker text, this uses a formatted string so our message could be localized        String tickerText = getString(R.string.imcoming_message_ticker_text, message);        // construct the Notification object.        Notification notif = new Notification(R.drawable.stat_sample, tickerText,                System.currentTimeMillis());        // Set the info for the views that show in the notification panel.        notif.setLatestEventInfo(this, from, message, contentIntent);        // We‘ll have this notification do the default sound, vibration, and led.        // Note that if you want any of these behaviors, you should always have        // a preference for the user to turn them off.        notif.defaults = Notification.DEFAULT_ALL;

 

4. 最後是觸發這個Notification:

        // Note that we use R.layout.incoming_message_panel as the ID for        // the notification.  It could be any integer you want, but we use        // the convention of using a resource id for a string related to        // the notification.  It will always be a unique number within your        // application.        nm.notify(R.string.imcoming_message_ticker_text, notif);

 

一般來說對應同一個事件可以使用同一個Notification來通知使用者,nm.notify的第一個參數為Notification 的ID,類型為整數。 可以使用同一個ID來表示同一個Notification,也可以使用這個ID來取消這個Notification,在IncomingMessage 中當使用者點擊顯示了這個IncomingMessage詳情後,會取消這個Notification(類IncomingMessageView中)。

nm.cancel(R.string.imcoming_message_ticker_text);  

 

 

【起航計劃 024】2015 起航計劃 Android APIDemo的魔鬼步伐 23 App->Notification->IncomingMessage 狀態列通知

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.