android Notification和NotificationManager的使用

來源:互聯網
上載者:User

標籤:android   style   blog   color   使用   strong   

Notification和NotificationManager

1.Broadcast Receiver組件沒有提供可視化的介面來顯示廣播資訊。這裡我們可以使用Notification和NotificationManager來實現可視化的資訊顯示。通過使用它們我們可以顯示廣播資訊的內容,表徵圖

以及震動等資訊。

2.使用Notification和NotificationManager也比較簡單,一般獲得系統級的服務NotificationManager,然後執行個體化Notification,設定其屬性,通過NotificationManager發出通知就可以了。

基本步驟如下:

  1)獲得系統級的服務NotificationManager,這裡比較簡單,通過Context.getSystemService()方法即可實現。

1 String service = Notification;2 NotificationManager nm = (NotificationManager)getSystemService(service);

  2)執行個體化Notification對象,並設定其屬性。

//執行個體化NotificationNotification n = new Notification();//誰知顯示表徵圖,表徵圖會在狀態列顯示int icon = n.icon = R.drawable.icon;//設定顯示提示資訊,String tickrtText = "Test Notification";//顯示時間long when = System.currentTimeMills();n.icon = icon;n.tickerText = tickerText;n.when = when;

  3)調用setLatestEcentInfo()方法在視圖中設定表徵圖和時間

  //執行個體化Intent

Intent intent = new Intent(this,MainActivity.class);//獲得 PendingIntentPendingIntent  pi = PendingIntent  .getActivity(this,0,intent,0);//設定時間資訊n.setLastEventInfo(this,"My content" , "My Content" , pi);

  4)發出通知

//表示該通知的IDint ID = 1;//發出通知nm.notify(ID,n);

3.下面直接通過一個例子來說明

 

 

 

 

 

 

 

聯繫我們

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