Android回想錄之通知欄

來源:互聯網
上載者:User

標籤:android   使用   io   檔案   資料   for   ar   cti   

通知欄

設定提醒標誌符Flags
功能:
提醒標誌符,向通知添加聲音、閃燈和震動效果等設定達到通知提醒效果,可以組合多個屬性
使用方法(其一):
Notification notify = mBuilder.build();
notify.flags = Notification.FLAG_AUTO_CANCEL;//
提醒標誌符成員:
Notification.FLAG_SHOW_LIGHTS //三色燈提醒,在使用三色燈提醒時候必須加該標誌符
Notification.FLAG_ONGOING_EVENT //發起正在運行事件(活動中)
Notification.FLAG_INSISTENT //讓聲音、震動無限迴圈,直到使用者響應 (取消或者開啟)
Notification.FLAG_ONLY_ALERT_ONCE //發起Notification後,鈴聲和震動均只執行一次
Notification.FLAG_AUTO_CANCEL //使用者單擊通知後自動消失
Notification.FLAG_NO_CLEAR //只有全部清除時,Notification才會清除 ,不清楚該通知(QQ的通知無法清除,就是用的這個)
Notification.FLAG_FOREGROUND_SERVICE //表示正在啟動並執行服務
附:
使用方法(其二):
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
PendingIntent pendingIntent= PendingIntent.getActivity(this, 1, new Intent(), flags);
mBuilder.setContentIntent(PendingIntent intent);

向通知添加聲音、閃燈和震動效果
功能:
向通知添加聲音、閃燈和震動效果的最簡單、最一致的方式是使用當前的使用者預設設定,使用defaults屬性.
可以組合多個屬性.
使用方法:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setDefaults(Notification.DEFAULT_LIGHTS|Notification.DEFAULT_SOUND);//不設定這個[響鈴,閃燈,震動]都不會執行,而是安靜.
對應屬性:
Notification.DEFAULT_VIBRATE //添加預設震動提醒 需要 VIBRATE permission
Notification.DEFAULT_SOUND // 添加預設聲音提醒
Notification.DEFAULT_LIGHTS// 添加預設三色燈提醒
Notification.DEFAULT_ALL// 添加預設以上3種全部提醒
不設定//這個[響鈴,閃燈,震動]都不會執行,而是安靜.

設定震動方式
使用方法(其一):
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setVibrate(new long[] {0,300,500,700});
使用方法(其二):
Notification notify = mBuilder.build();
notify.vibrate = new long[] {0,300,500,700};
效果:
延遲0ms,然後震動300ms,在延遲500ms,接著在震動700ms。

三色燈提醒
功能:
android支援三色燈提醒,這個方法就是設定不同情境下的不同顏色的燈。
使用方法(其一):
mBuilder.setLights(0xff0000ff, 300, 0);
使用方法(其二):
Notification notify = mBuilder.build();
notify.flags = Notification.FLAG_SHOW_LIGHTS;
notify.ledARGB = 0xff0000ff;
notify.ledOnMS = 300;
notify.ledOffMS = 300;
描述:
其中ledARGB 表示燈光顏色、 ledOnMS 亮期間、ledOffMS 暗的時間。
注意:
1)只有在設定了標誌符Flags為Notification.FLAG_SHOW_LIGHTS的時候,才支援三色燈提醒。
2)這邊的顏色跟裝置有關,不是所有的顏色都可以,要看具體裝置。

設定提醒聲音
使用方法(其一):
//設定預設鈴聲
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
//設定自訂鈴聲
mBuilder.setSound(Uri.parse("file:///sdcard/xx/xx.mp3"));
//擷取Android多媒體庫內的鈴聲
mBuilder.setSound(Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "5"));
使用方法(其二):
Notification notify = mBuilder.build();
notify.sound = Uri.parse("file:///sdcard/xx/xx.mp3");

設定優先權
使用方法(其一):
mBuilder.setPriority(int pri);
使用方法(其二):
Notification notify = mBuilder.build();
notify.priority = Notification.PRIORITY_DEFAULT;
可選屬性:
Notification.PRIORITY_DEFAULT
Notification.PRIORITY_HIGH
Notification.PRIORITY_LOW
Notification.PRIORITY_MAX
Notification.PRIORITY_MIN

進行中的通知
使用方法(唯一):
mBuilder.setOngoing(false);
功能:
設定為ture,表示它為一個進行中的通知。
他們通常是用來表示一個背景工作,使用者積极參与(如播放音樂)或以某種方式正在等待,因此佔用裝置(如一個檔案下載,同步操作,主動網路連接).

進度條
功能:
設定帶進度條的通知,可以在下載中使用.
使用方法(唯一):
mBuilder.setProgress(int max, int progress,boolean indeterminate)

響應[點擊,刪除,緊急]事件
方法(其一):
//點擊
mBuilder.setContentIntent(PendingIntent intent);
//清空
mBuilder.setDeleteIntent(PendingIntent intent);
//響應緊急事件
setFullScreenIntent(PendingIntent intent, boolean highPriority)
方法(其二):
Notification notify = mBuilder.build();
notify.contentIntent:在通知視窗地區,Notification被單擊時的響應事件由該intent觸發;
notify.deleteIntent:當使用者點擊全部清除按鈕時,響應該清除事件的Intent;
notify.fullScreenIntent:響應緊急狀態的全屏事件(例如來電事件),也就是說通知來的時候,跳過在通知區域點擊通知這一步,直接執行fullScreenIntent代表的事件。
附:
PendingIntent的位標識符:
FLAG_ONE_SHOT 表示返回的PendingIntent僅能執行一次,執行完後自動取消
FLAG_NO_CREATE 表示如果描述的PendingIntent不存在,並不建立相應的PendingIntent,而是返回NULL
FLAG_CANCEL_CURRENT 表示相應的PendingIntent已經存在,則取消前者,然後建立新的PendingIntent,這個有利於資料保持為最新的,可以用於即時通訊的通訊情境
FLAG_UPDATE_CURRENT 表示更新的PendingIntent

如何取消掉通知欄上的通知

1.設定對應的flags,讓使用者點擊既被消除:
notification.flags = FLAG_AUTO_CANCEL;
2.通過手動消除某項或則全部通知
mNotificationMgr.cancle(NOTIFICATION_ID);//消除對應ID的通知
mNotificationMgr.cancleAll();//消除建立的所有通知

聯繫我們

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