安卓Notification通知欄全解

全棧工程師開發手冊 (作者:欒鵬)
安卓教程全解

安卓通知欄是提醒用戶信息有效手段,也是通過用戶的行爲觸發事件服務的方式。

Notification允許在當前應用程序不活動或不可見時向用戶發送信號。

創建通知欄有使用Notification直接創建何使用Notification.Builder創建兩種方法

1、使用Notification直接創建通知欄

private Notification simpleNotification() {
	    //選擇一個drawable來作爲狀態欄圖標顯示
	    int icon = R.drawable.icon;
	    //當啓動通知時在狀態欄中顯示的文本
	    String tickerText = "通知顯示文本";
	    //展開的狀態欄按時間順序排序通知
	    long when = System.currentTimeMillis();  //指定時間戳
	    Notification notification = new Notification(icon, tickerText, when);
	    //notification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;   //將默認的聲音和神東賦值給notification
	    Uri ringUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);  //獲取系統某音頻文件的uri
	    notification.sound = ringUri;   //設置notification的提示聲音
	    notification.flags=notification.flags|Notification.FLAG_ONGOING_EVENT;  //設置爲持續的notification
	    //notification.flags=notification.flags|Notification.FLAG_INSISTENT;  //建議不要使用,設置爲連續的notification,會一直重複音頻、震動和閃燈設置,直到被取消.
	    return notification;
	  }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章