Android開發技術-Notification與NotificationManager詳細介紹

來源:互聯網
上載者:User

標籤:

在Android系統中,發一個狀態列通知還是很方便的。下面我們就來看一下,怎麼發送狀態列通知,狀態列通知又有哪些參數可以設定?
首先,發送一個狀態列通知必須用到兩個類:NotificationManager、Notification。
NotificationManager:是狀態列通知的管理類,負責發通知、清楚通知等。
NotificationManager是一個系統Service,必須通過getSystemService()方法來擷取。
NotificationManagernm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification:是具體的狀態列通知對象,可以設定icon、文字、提示聲音、震動等等參數。
下面是設定一個通知需要的基本參數:
Anicon(通知的表徵圖)
Atitleandexpandedmessage(通知的標題和內容)
APendingIntent(點擊通知執行頁面跳轉)
一、建立Notification
通過NotificationManager的notify(int,Notification)方法來啟動Notification。
第一個參數唯一的標識該Notification,第二個參數就是Notification對象。
二、更新Notification
調用Notification的setLatestEventInfo方法來更新內容,然後再調用NotificationManager的notify()方法即可。(具體可以看下面的執行個體)
三、刪除Notification
通過NotificationManager的cancel(int)方法,來清除某個通知。其中參數就是Notification的唯一標識ID。
當然也可以通過cancelAll()來清除狀態列所有的通知。
四、Notification設定(震動、鈴聲等)
1.基本設定:
代碼如下:

    //建立狀態列通知    baseNF=newNotification();    //設定通知在狀態列顯示的表徵圖    baseNF.icon=R.drawable.icon;    //通知時在狀態列顯示的內容    baseNF.tickerText="YouclickedBaseNF!";    //通知的預設參數DEFAULT_SOUND,DEFAULT_VIBRATE,DEFAULT_LIGHTS.    //如果要全部採用預設值,用DEFAULT_ALL.    //此處採用預設聲音    baseNF.defaults=Notification.DEFAULT_SOUND;    //第二個參數:下拉狀態列時顯示的訊息標題expandedmessagetitle    //第三個參數:下拉狀態列時顯示的訊息內容expandedmessagetext    //第四個參數:點擊該通知時執行頁面跳轉    baseNF.setLatestEventInfo(Lesson_10.this,"Title01","Content01",pd);    //發出狀態列通知    //ThefirstparameteristheuniqueIDfortheNotification    //andthesecondistheNotificationobject.    nm.notify(Notification_ID_BASE,baseNF);

2.添加聲音
如果要採用預設聲音,只要使用default就可以了。
代碼如下:

    baseNF.defaults=Notification.DEFAULT_SOUND;

如果要使用自訂聲音,那麼就要用到sound了。如下:
代碼如下:

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

上面這種方法,使用的是自己的鈴聲,如果想用系統內建的鈴聲,可以這樣:
代碼如下:

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

需要注意一點,如果default、sound同時出現,那麼sound無效,會使用預設鈴聲。
預設情況下,通知的聲音播放一遍就會結束。如果你想讓聲音迴圈播放,需要為flags參數加上FLAG_INSISTENT。這樣聲音會到使用者響應才結束,比如下拉狀態列。
代碼如下:

    notification.flags|=notification.FLAG_INSISTENT;

3.添加震動
如果是使用預設的震動方式,那麼同樣也是使用default。
代碼如下:

notification.defaults|=Notification.DEFAULT_VIBRATE;

當然也可以自己定義震動形式,這邊需要用到Long型數組。
代碼如下:

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

這邊的Long型數組中,第一個參數是開始震動前等待的時間,第二個參數是第一次震動的時間,第三個參數是第二次震動的時間,以此類推,隨便定義多長的數組。但是採用這種方法,沒有辦法做到重複震動。
同樣,如果default、vibrate同時出現時,會採用預設形式。
另外還需要注意一點:使用震動器時需要許可權,如下:
代碼如下:

    <uses-permissionandroid:name="android.permission.VIBRATE"></uses-permission>

4.閃光
使用預設的燈光,如下:
代碼如下:

    notification.defaults|=Notification.DEFAULT_LIGHTS;

自訂:
代碼如下:

    notification.ledARGB=0xff00ff00;    notification.ledOnMS=300;    notification.ledOffMS=1000;    notification.flags|=Notification.FLAG_SHOW_LIGHTS;

其中ledARGB表示燈光顏色、ledOnMS亮期間、ledOffMS暗的時間。
注意:這邊的顏色跟裝置有關,不是所有的顏色都可以,要看具體裝置。
5.其他有用的設定:
代碼如下:
flags:
Notification.FLAG_INSISTENT;//讓聲音、震動無限迴圈,直到使用者響應
Notification.FLAG_AUTO_CANCEL;//通知被點擊後,自動消失
Notification.FLAG_NO_CLEAR;//點擊’Clear’時,不清楚該通知(QQ的通知無法清除,就是用的這個
//自訂下拉視圖,比如下載軟體時,顯示的進度條。
Notificationnotification=newNotification();
notification.icon=R.drawable.icon;
notification.tickerText=”Custom!”;
RemoteViewscontentView=newRemoteViews(getPackageName(),R.layout.custom);
contentView.setImageViewResource(R.id.image,R.drawable.icon);
contentView.setTextViewText(R.id.text,”Hello,thismessageisinacustomexpandedview”);
notification.contentView=contentView;
//使用自訂下拉視圖時,不需要再調用setLatestEventInfo()方法
//但是必須定義contentIntent
notification.contentIntent=pd;
nm.notify(3,notification);

Android開發技術-Notification與NotificationManager詳細介紹

聯繫我們

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