Android Notification學習

來源:互聯網
上載者:User

標籤:

  Notication就是通知,用在有電話,簡訊時,在手機提示欄中出現一個小表徵圖,提示使用者去處理這個通知。

  Notication有兩種視圖模式,即普通模式和大圖模式,這裡就只講普通模式。

  

  正如所示,在通知欄的Notification中,1代表的是標題,2的是大的icon(貌似這裡要是位元影像),3的是文本,4的是一些額外的資訊,5的是小的icon,6的是通知的時間。

  

  因為官方文檔已經棄用了Notification類中的一些方法,提倡用其內部類Builder來擷取Notification,因此這裡就使用Builder。

  1.擷取NotificationManager,這裡官方文檔推薦使用getSystemService()來擷取,由作業系統來管理

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

  2.建立一個Builder

    Notification.Builder builder = new Notification.Builder(UsingNotification.this);

  3.為通知設定屬性,如設定標題,內容,點擊事件等等

    builder.setTicker("Test").setContentTitle("android學習").setContentText("學習Notification");

 

    設定點擊事件,點擊通知時跳轉到Other這個activity

    Intent intent = new Intent(UsingNotification.this, Other.class);

    PendingIntent pendingIntent = PendingIntent.getActivity(UsingNotification.this, 0, intent, 0);

    builder..setContentIntent(pendingIntent);

    還有其它屬性參考http://wear.techbrood.com/reference/android/app/Notification.Builder.html

  4.產生Notification

    Notification notification = builder.build();

    這裡需要注意的是build()這個方法需要API 16及以上

  

  5.讓管理器發布這個通知

    // notify()的第一個參數是該notification的唯一ID,與該Notification綁定在一起
    manager.notify(1, notification);

 

  效果

  

  

  如有不正確還望指出,多謝~

  

  參考文章Android技巧小結之新舊版本Notification

 

    

Android Notification學習

聯繫我們

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