android Notification介紹,androidnotification

來源:互聯網
上載者:User

android Notification介紹,androidnotification

如果要添加一個Notification,可以按照以下幾個步驟

1:擷取NotificationManager:

NotificationManager m_NotificationManager=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);

2:定義一個Notification:

  Notification  m_Notification=new Notification();

3:設定Notification的各種屬性:

 //設定通知在狀態列顯示的表徵圖 m_Notification.icon=R.drawable.icon;                   

//當我們點擊通知時顯示的內容 m_Notification.tickerText="Button1 通知內容.....";                                 

通知時發出的預設聲音 m_Notification.defaults=Notification.DEFAULT_SOUND;

//設定通知顯示的參數

Intent   m_Intent=new Intent(NotificationDemo.this,DesActivity.class);        PendingIntent m_PendingIntent=PendingIntent.getActivity(NotificationDemo.this, 0, m_Intent, 0);

m_Notification.setLatestEventInfo(NotificationDemo.this, "Button1", "Button1通知",m_PendingIntent );

//這個可以理解為開始執行這個通知 m_NotificationManager.notify(0,m_Notification);

4:既然可以增加同樣我們也可以刪除。當然是只是刪除你自己增加的。

  m_NotificationManager.cancel(0);   

  這裡的0是一個ID號碼,和notify第一個參數0一樣。

這也就完成了,添加刪除工作。

 

NoticificationManager很容易可以放在狀態列,也很容易實現從statusbar進入程式 中,  NoticificationManager中通過intent執行此程式的activity就可以了

NoticificationManager狀態列操作

NotificationManager(通知管理器):  NotificationManager負責通知使用者事件的發生.  NotificationManager有三個公用方法:  1. cancel(int id) 取消以前顯示的一個通知.假如是一個短暫的通知,試圖將隱藏,假如是一個持久的通知,將從狀態條中移走.  2. cancelAll() 取消以前顯示的所有通知.  3. notify(int id,  Notification notification) 把通知持久的發送到狀態條上.

//初始化NotificationManager:  NotificationManager nm =        (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

Notification代表著一個通知.  Notification的屬性:  audioStreamType 當聲音響起時,所用的音頻流的類型  contentIntent 當通知條目被點擊,就執行這個被設定的Intent.  contentView 當通知被顯示在狀態條上的時候,同時這個被設定的視圖被顯示.  defaults 指定哪個值要被設定成預設的.  deleteIntent 當使用者點擊"Clear All Notifications"按鈕區刪除所有的通知的時候,這個被設定的Intent被執行.  icon 狀態條所用的圖片.  iconLevel 假如狀態條的圖片有幾個層級,就設定這裡.  ledARGB LED燈的顏色.  ledOffMS LED關閉時的閃光時間(以毫秒計算)  ledOnMS LED開始時的閃光時間(以毫秒計算)  number 這個通知代表事件的號碼  sound 通知的聲音  tickerText 通知被顯示在狀態條時,所顯示的資訊  vibrate 震動模式.  when 通知的時間戳記.

將Notification發送到狀態條上:  Notification notification = new Notification();  Notification的設定過程……..  nm.notify(0, notification);   //發送到狀態條上

 

------------------------------------------------------------------------------------------------------------

Notification提供了豐富的手機提示方式:

a)在狀態列(Status Bar)顯示的通知文本提示,如:

notification.tickerText = "hello";

 

b)發出提示音,如:

notification.defaults = Notification.DEFAULT_SOUND;

notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");

notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");

 

c)手機震動,如:

notification.defaults = Notification.DEFAULT_VIBRATE;

long[] vibrate = {0,100,200,300};

notification.vibrate = vibrate;

 

d)LED燈閃爍,如:

notification.defaults = Notification.DEFAULT_LIGHTS;

notification.ledARGB = 0xff00ff00;

notification.ledOnMS = 300;

notification.ledOffMS = 1000;

notification.flags = Notification.FLAG_SHOW_LIGHTS;

4)發送通知:

private static final int ID_NOTIFICATION = 1;

mNotificationManager.notify(ID_NOTIFICATION, 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.