日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android Notification 详解——基本操作

發(fā)布時間:2024/1/8 Android 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android Notification 详解——基本操作 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?

Android Notification 詳解

版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。

前幾天項目中有用到 Android 通知相關(guān)的內(nèi)容,索性把 Android Notification 相關(guān)的知識都看了一遍,稍作梳理,在此做個總結(jié),以備不時之需。

溫故而知新,可以為師矣~

下圖是我對 Notification 做的思維導(dǎo)圖,也是本文的主要邏輯。

本文主要講述 Notification 的基本操作部分,進階部分的內(nèi)容還在學(xué)習(xí)ing~

Notification 概述

Notification,是一種具有全局效果的通知,可以在系統(tǒng)的通知欄中顯示。當(dāng) APP 向系統(tǒng)發(fā)出通知時,它將先以圖標(biāo)的形式顯示在通知欄中。用戶可以下拉通知欄查看通知的詳細信息。通知欄和抽屜式通知欄均是由系統(tǒng)控制,用戶可以隨時查看。下面兩張圖均是來自 Google 官方文檔。


圖 1 .通知欄中的通知


圖 2 .抽屜式通知欄中的通知

通知的目的是告知用戶 App 事件。在平時的使用中,通知主要有以下幾個作用:

  • 顯示接收到短消息、及時消息等信息(如QQ、微信、新浪、短信)
  • 顯示客戶端的推送消息,如廣告、優(yōu)惠、版本更新、推薦新聞等,常用的第三方 SDK 有: JPush 、 個推 、 信鴿 、 網(wǎng)易云信(偏重 IM )阿里云推送
  • 顯示正在進行的事物,例如:后臺運行的程序,如音樂播放進度、下載進度等
  • 其中,前兩點可以歸結(jié)為與用戶交互,第三點是實時的任務(wù)提醒,但不可否認(rèn)的是,第三點也會與用戶交互。

    Notification 作為 Android 重要的用戶界面組成部分,它有自己的設(shè)計指南。在 Android 5.0(Api level 21) 中引入的 Material Design 尤為重要。關(guān)于 Notification 的設(shè)計指南請參考 Notification Pattern

    Notification 的概述就這么多,接下去就開始講 Notification 的基本使用,中間會穿插 Notification 的基本 UI 、各個版本的區(qū)別、常見的通知效果以及自己在學(xué)習(xí)過程中踩到的坑。

    Notification 的基本操作

    Notification 的基本操作主要有創(chuàng)建、更新、取消這三種。一個 Notification 的必要屬性有三項,如果不設(shè)置則在運行時會拋出異常:

  • 小圖標(biāo),通過 setSmallIcon() 方法設(shè)置
  • 標(biāo)題,通過 setContentTitle() 方法設(shè)置
  • 內(nèi)容,通過 setContentText() 方法設(shè)置
  • 除了以上三項,其它均為可選項。雖然如此,但還是應(yīng)該給 Notification 設(shè)置一個 Action ,這樣就可以直接跳轉(zhuǎn)到 App 的某個 Activity 、啟動一個 Service 或者發(fā)送一個 Broadcast。否則,Notification 僅僅只能起到通知的效果,而不能與用戶交互。

    當(dāng)系統(tǒng)接收到通知時,可以通過震動、響鈴、呼吸燈等多種方式進行提醒。

    創(chuàng)建 Notification

    Notification 的創(chuàng)建主要涉及到 Notification.BuilderNotification 、 NotificationManager 。

  • Notification.Builer : 使用建造者模式構(gòu)建 Notification 對象。由于 Notification.Builder 僅支持 Android 4.1及之后的版本,為了解決兼容性問題, Google 在 Android Support v4 中加入了 NotificationCompat.Builder 類。對于某些在 Android 4.1 之后才特性,即使 NotificationCompat.Builder 支持該方法,在之前的版本中也不能運行。點我 查看更多關(guān)于 Notification 兼容性問題處理。文中使用的都是 NotificationCompat。
  • Notification : 通知對應(yīng)類,保存通知相關(guān)的數(shù)據(jù)。NotificationManager 向系統(tǒng)發(fā)送通知時會用到。
  • NotificationManager : NotificationManager 是通知管理類,它是一個系統(tǒng)服務(wù)。調(diào)用 NotificationManager 的 notify() 方法可以向系統(tǒng)發(fā)送通知。
  • 獲取 NotificationManager 對象:

    NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    前面講到,Notification 有三個必要屬性。下面,我們就來創(chuàng)建一個簡單的 Notification 。主要有以下三步:

  • 獲取 NotificationManager 實例
  • 實例化 NotificationCompat.Builder 并設(shè)置相關(guān)屬性
  • 通過 builder.build() 方法生成 Notification 對象,并發(fā)送通知
  • private void sendNotification() {//獲取NotificationManager實例NotificationManager notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);//實例化NotificationCompat.Builde并設(shè)置相關(guān)屬性NotificationCompat.Builder builder = new NotificationCompat.Builder(this)//設(shè)置小圖標(biāo).setSmallIcon(R.mipmap.icon_fab_repair)//設(shè)置通知標(biāo)題.setContentTitle("最簡單的Notification")//設(shè)置通知內(nèi)容.setContentText("只有小圖標(biāo)、標(biāo)題、內(nèi)容")//設(shè)置通知時間,默認(rèn)為系統(tǒng)發(fā)出通知的時間,通常不用設(shè)置//.setWhen(System.currentTimeMillis());//通過builder.build()方法生成Notification對象,并發(fā)送通知,id=1notifyManager.notify(1, builder.build()); }

    以上代碼是對 Android 3.0 及之后的版本而言(包括使用 Support Library),對于 Android 3.0 之前的版本,主要使用 new Notification() 方法來創(chuàng)建 Notification 對象,本文不對此方式做任何講解,代碼如下:

    NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, ResultActivity.class), 0);Notification notification = new Notification(icon, tickerText, when); notification.setLatestEventInfo(this, title, content, contentIntent);mNotifyMgr.notify(NOTIFICATIONS_ID, notification);

    補充

    • Android Support Library包的區(qū)別
    Android Support v4:這個包是為了照顧1.6及更高版本而設(shè)計的,這個包是使用最廣泛的。Android Support v7:這個包是為了考慮照顧2.1及以上版本而設(shè)計的,但不包含更低,故如果不考慮1.6,我們可以采用再加上這個包,另外注意,v7是要依賴v4這個包的,即,兩個得同時被包含。Android Support v13:這個包的設(shè)計是為了android 3.2及更高版本的,一般我們都不常用,平板開發(fā)中能用到。
    • Notification 中的元素。在 Android N(24) 中, Google 對 Notification 的 UI 進行了修改。下圖是 Android M 和 Android N 的對比。
    • 關(guān)于 setSmallIcon() 與 setLargeIcon()。在 NotificationCompat.Builder 中有設(shè)置通知的大小圖標(biāo)的兩個方法。這兩個方法有什么區(qū)別呢?當(dāng) setSmallIcon() 與 setLargeIcon() 同時存在時, smallIcon 顯示在通知的右下角, largeIcon 顯示在左側(cè);當(dāng)只設(shè)置 setSmallIcon() 時, smallIcon 顯示在左側(cè)。看下圖你就明白了。對于部分 ROM ,可能修改過源碼,如 MIUI 上通知的大圖標(biāo)和小圖標(biāo)是沒有區(qū)別的。

    Google 官方是這么解釋 setSmallIcon() 這個方法的:

    Set the small icon resource, which will be used to represent the notification in the status bar. The platform template for the expanded view will draw this icon in the left, unless a large icon has also been specified, in which case the small icon will be moved to the right-hand side.

    給 Notification 設(shè)置 Action

    在前一章節(jié) 創(chuàng)建 Notification 中發(fā)送的通知并不具備與用戶交互的能力,這是因為我們并沒有給 Notification 設(shè)置 Action 。在這一節(jié),我們就來講講如何給 Notification 設(shè)置 Action 。這里,我們來實現(xiàn)一個點擊 Notification 跳轉(zhuǎn)到 MainActivity 的效果。代碼如下:

    /** * 發(fā)送一個點擊跳轉(zhuǎn)到MainActivity的消息 */ private void sendSimplestNotificationWithAction() {//獲取PendingIntentIntent mainIntent = new Intent(this, MainActivity.class);PendingIntent mainPendingIntent = PendingIntent.getActivity(this, 0, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);//創(chuàng)建 Notification.Builder 對象NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_launcher)//點擊通知后自動清除.setAutoCancel(true).setContentTitle("我是帶Action的Notification").setContentText("點我會打開MainActivity").setContentIntent(mainPendingIntent);//發(fā)送通知mNotifyManager.notify(3, builder.build()); }

    相比發(fā)送最簡單的通知,發(fā)送具有 Action 的通知多了創(chuàng)建 Intent 、 PendingIntent 和 setContentIntent() 這幾步。
    不難看出, PendingIntent 才是重點,那么, PendingIntent 是什么呢?

    PendingIntent

    如果您了解 PendingIntent ,請直接跳過本節(jié)。

    PendingIntent 是一種特殊的 Intent ,字面意思可以解釋為延遲的 Intent ,用于在某個事件結(jié)束后執(zhí)行特定的 Action 。從上面帶 Action 的通知也能驗證這一點,當(dāng)用戶點擊通知時,才會執(zhí)行。
    PendingIntent 是 Android 系統(tǒng)管理并持有的用于描述和獲取原始數(shù)據(jù)的對象的標(biāo)志(引用)。也就是說,即便創(chuàng)建該PendingIntent對象的進程被殺死了,這個PendingItent對象在其他進程中還是可用的
    日常使用中的短信、鬧鐘等都用到了 PendingIntent。

    PendingIntent 主要可以通過以下三種方式獲取:

    //獲取一個用于啟動 Activity 的 PendingIntent 對象 public static PendingIntent getActivity(Context context, int requestCode, Intent intent, int flags);//獲取一個用于啟動 Service 的 PendingIntent 對象 public static PendingIntent getService(Context context, int requestCode, Intent intent, int flags);//獲取一個用于向 BroadcastReceiver 廣播的 PendingIntent 對象 public static PendingIntent getBroadcast(Context context, int requestCode, Intent intent, int flags)

    PendingIntent 具有以下幾種 flag:

    FLAG_CANCEL_CURRENT:如果當(dāng)前系統(tǒng)中已經(jīng)存在一個相同的 PendingIntent 對象,那么就將先將已有的 PendingIntent 取消,然后重新生成一個 PendingIntent 對象。FLAG_NO_CREATE:如果當(dāng)前系統(tǒng)中不存在相同的 PendingIntent 對象,系統(tǒng)將不會創(chuàng)建該 PendingIntent 對象而是直接返回 null 。FLAG_ONE_SHOT:該 PendingIntent 只作用一次。FLAG_UPDATE_CURRENT:如果系統(tǒng)中已存在該 PendingIntent 對象,那么系統(tǒng)將保留該 PendingIntent 對象,但是會使用新的 Intent 來更新之前 PendingIntent 中的 Intent 對象數(shù)據(jù),例如更新 Intent 中的 Extras 。

    更新 Notification

    更新通知很簡單,只需要再次發(fā)送相同 ID 的通知即可,如果之前的通知還未被取消,則會直接更新該通知相關(guān)的屬性;如果之前的通知已經(jīng)被取消,則會重新創(chuàng)建一個新通知。

    更新通知跟發(fā)送通知使用相同的方式。詳見上節(jié):創(chuàng)建 Notification

    取消 Notification

    取消通知有如下 5 種方式:

  • 點擊通知欄的清除按鈕,會清除所有可清除的通知
  • 設(shè)置了 setAutoCancel() 或 FLAG_AUTO_CANCEL 的通知,點擊該通知時會清除它
  • 通過 NotificationManager 調(diào)用 cancel(int id) 方法清除指定 ID 的通知
  • 通過 NotificationManager 調(diào)用 cancel(String tag, int id) 方法清除指定 TAG 和 ID 的通知
  • 通過 NotificationManager 調(diào)用 cancelAll() 方法清除所有該應(yīng)用之前發(fā)送的通知
  • 如果你是通過 NotificationManager.notify(String tag, int id, Notification notify) 方法創(chuàng)建的通知,那么只能通過 NotificationManager.cancel(String tag, int id) 方法才能清除對應(yīng)的通知,調(diào)用NotificationManager.cancel(int id) 無效。

    關(guān)于 Notification 的基本操作代碼如下,布局文件代碼這就不貼了。我是 Demo 傳送門

    /*** 為了方便,大部分通知都沒設(shè)置對應(yīng)的Action,即PendingIntent* 除了sendFlagAutoCancelNotification()方法*/ public class SimpleNotificationActivity extends Activity implements View.OnClickListener {//Notification.FLAG_FOREGROUND_SERVICE //表示正在運行的服務(wù)public static final String NOTIFICATION_TAG = "littlejie";public static final int DEFAULT_NOTIFICATION_ID = 1;private NotificationManager mNotificationManager;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_simple_notification);findViewById(R.id.btn_remove_all_notification).setOnClickListener(this);findViewById(R.id.btn_send_notification).setOnClickListener(this);findViewById(R.id.btn_remove_notification).setOnClickListener(this);findViewById(R.id.btn_send_notification_with_tag).setOnClickListener(this);findViewById(R.id.btn_remove_notification_with_tag).setOnClickListener(this);findViewById(R.id.btn_send_ten_notification).setOnClickListener(this);findViewById(R.id.btn_send_flag_no_clear_notification).setOnClickListener(this);findViewById(R.id.btn_send_flag_ongoing_event_notification).setOnClickListener(this);findViewById(R.id.btn_send_flag_auto_cancecl_notification).setOnClickListener(this);mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btn_remove_all_notification://移除當(dāng)前 Context 下所有 Notification,包括 FLAG_NO_CLEAR 和 FLAG_ONGOING_EVENTmNotificationManager.cancelAll();break;case R.id.btn_send_notification://發(fā)送一個 Notification,此處 ID = 1sendNotification();break;case R.id.btn_remove_notification://移除 ID = 1 的 Notification,注意:該方法只針對當(dāng)前 Context。mNotificationManager.cancel(DEFAULT_NOTIFICATION_ID);break;case R.id.btn_send_notification_with_tag://發(fā)送一個 ID = 1 并且 TAG = littlejie 的 Notification//注意:此處發(fā)送的通知與 sendNotification() 發(fā)送的通知并不沖突//因為此處的 Notification 帶有 TAGsendNotificationWithTag();break;case R.id.btn_remove_notification_with_tag://移除一個 ID = 1 并且 TAG = littlejie 的 Notification//注意:此處移除的通知與 NotificationManager.cancel(int id) 移除通知并不沖突//因為此處的 Notification 帶有 TAGmNotificationManager.cancel(NOTIFICATION_TAG, DEFAULT_NOTIFICATION_ID);break;case R.id.btn_send_ten_notification://連續(xù)發(fā)十條 NotificationsendTenNotifications();break;case R.id.btn_send_flag_no_clear_notification://發(fā)送 ID = 1, flag = FLAG_NO_CLEAR 的 Notification//下面兩個 Notification 的 ID 都為 1,會發(fā)現(xiàn) ID 相等的 Notification 會被最新的替換掉sendFlagNoClearNotification();break;case R.id.btn_send_flag_auto_cancecl_notification:sendFlagOngoingEventNotification();break;case R.id.btn_send_flag_ongoing_event_notification:sendFlagAutoCancelNotification();break;}}/*** 發(fā)送最簡單的通知,該通知的ID = 1*/private void sendNotification() {//這里使用 NotificationCompat 而不是 Notification ,因為 Notification 需要 API 16 才能使用//NotificationCompat 存在于 V4 Support LibraryNotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("Send Notification").setContentText("Hi,My id is 1");mNotificationManager.notify(DEFAULT_NOTIFICATION_ID, builder.build());}/*** 使用notify(String tag, int id, Notification notification)方法發(fā)送通知* 移除對應(yīng)通知需使用 cancel(String tag, int id)*/private void sendNotificationWithTag() {NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("Send Notification With Tag").setContentText("Hi,My id is 1,tag is " + NOTIFICATION_TAG);mNotificationManager.notify(NOTIFICATION_TAG, DEFAULT_NOTIFICATION_ID, builder.build());}/*** 循環(huán)發(fā)送十個通知*/private void sendTenNotifications() {for (int i = 0; i < 10; i++) {NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("Send Notification Batch").setContentText("Hi,My id is " + i);mNotificationManager.notify(i, builder.build());}}/*** 設(shè)置FLAG_NO_CLEAR* 該 flag 表示該通知不能被狀態(tài)欄的清除按鈕給清除掉,也不能被手動清除,但能通過 cancel() 方法清除* Notification.flags屬性可以通過 |= 運算疊加效果*/private void sendFlagNoClearNotification() {NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("Send Notification Use FLAG_NO_CLEAR").setContentText("Hi,My id is 1,i can't be clear.");Notification notification = builder.build();//設(shè)置 Notification 的 flags = FLAG_NO_CLEAR//FLAG_NO_CLEAR 表示該通知不能被狀態(tài)欄的清除按鈕給清除掉,也不能被手動清除,但能通過 cancel() 方法清除//flags 可以通過 |= 運算疊加效果notification.flags |= Notification.FLAG_NO_CLEAR;mNotificationManager.notify(DEFAULT_NOTIFICATION_ID, notification);}/*** 設(shè)置FLAG_AUTO_CANCEL* 該 flag 表示用戶單擊通知后自動消失*/private void sendFlagAutoCancelNotification() {//設(shè)置一個Intent,不然點擊通知不會自動消失Intent resultIntent = new Intent(this, MainActivity.class);PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("Send Notification Use FLAG_AUTO_CLEAR").setContentText("Hi,My id is 1,i can be clear.").setContentIntent(resultPendingIntent);Notification notification = builder.build();//設(shè)置 Notification 的 flags = FLAG_NO_CLEAR//FLAG_AUTO_CANCEL 表示該通知能被狀態(tài)欄的清除按鈕給清除掉//等價于 builder.setAutoCancel(true);notification.flags |= Notification.FLAG_AUTO_CANCEL;mNotificationManager.notify(DEFAULT_NOTIFICATION_ID, notification);}/*** 設(shè)置FLAG_ONGOING_EVENT* 該 flag 表示發(fā)起正在運行事件(活動中)*/private void sendFlagOngoingEventNotification() {NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("Send Notification Use FLAG_ONGOING_EVENT").setContentText("Hi,My id is 1,i can't be clear.");Notification notification = builder.build();//設(shè)置 Notification 的 flags = FLAG_NO_CLEAR//FLAG_ONGOING_EVENT 表示該通知通知放置在正在運行,不能被手動清除,但能通過 cancel() 方法清除//等價于 builder.setOngoing(true);notification.flags |= Notification.FLAG_ONGOING_EVENT;mNotificationManager.notify(DEFAULT_NOTIFICATION_ID, notification);} }

    設(shè)置 Notification 的通知效果

    前面講了 Notification 的創(chuàng)建、更新和取消,以及給 Notification 設(shè)置 Action 等基本操作。那么,我怎么給 Notification 設(shè)置諸如震動、鈴聲、呼吸燈等效果呢?別急,接下來馬上就會告訴你怎么給 Notification 添加效果。

    Notification 有震動、響鈴、呼吸燈三種響鈴效果,可以通過 setDefaults(int defualts) 方法來設(shè)置。 Default 屬性有以下四種,一旦設(shè)置了 Default 效果,自定義的效果就會失效。樓主在這里踩了坑,愣是調(diào)了半天沒找到為什么自定義效果會消失,忘大家慎之。

    //設(shè)置系統(tǒng)默認(rèn)提醒效果,一旦設(shè)置默認(rèn)提醒效果,則自定義的提醒效果會全部失效。具體可看源碼 //添加默認(rèn)震動效果,需要申請震動權(quán)限 //<uses-permission android:name="android.permission.VIBRATE" /> Notification.DEFAULT_VIBRATE//添加系統(tǒng)默認(rèn)聲音效果,設(shè)置此值后,調(diào)用setSound()設(shè)置自定義聲音無效 Notification.DEFAULT_SOUND//添加默認(rèn)呼吸燈效果,使用時須與 Notification.FLAG_SHOW_LIGHTS 結(jié)合使用,否則無效 Notification.DEFAULT_LIGHTS//添加上述三種默認(rèn)提醒效果 Notification.DEFAULT_ALL

    除了以上幾種設(shè)置 Notification 默認(rèn)通知效果,還可以通過以下幾種 FLAG 設(shè)置通知效果。

    //提醒效果常用 Flag //三色燈提醒,在使用三色燈提醒時候必須加該標(biāo)志符 Notification.FLAG_SHOW_LIGHTS//發(fā)起正在運行事件(活動中) Notification.FLAG_ONGOING_EVENT//讓聲音、振動無限循環(huán),直到用戶響應(yīng) (取消或者打開) Notification.FLAG_INSISTENT//發(fā)起Notification后,鈴聲和震動均只執(zhí)行一次 Notification.FLAG_ONLY_ALERT_ONCE//用戶單擊通知后自動消失 Notification.FLAG_AUTO_CANCEL//只有調(diào)用NotificationManager.cancel()時才會清除 Notification.FLAG_NO_CLEAR//表示正在運行的服務(wù) Notification.FLAG_FOREGROUND_SERVICE

    Notification 通知效果的設(shè)置方式及注意事項全部在代碼中,核心代碼如下:

    /** * 最普通的通知效果 */ private void showNotifyOnlyText() {NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_launcher).setLargeIcon(mLargeIcon).setContentTitle("我是只有文字效果的通知").setContentText("我沒有鈴聲、震動、呼吸燈,但我就是一個通知");mManager.notify(1, builder.build()); }/** * 展示有自定義鈴聲效果的通知 * 補充:使用系統(tǒng)自帶的鈴聲效果:Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6"); */ private void showNotifyWithRing() {NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("我是伴有鈴聲效果的通知").setContentText("美妙么?安靜聽~")//調(diào)用系統(tǒng)默認(rèn)響鈴,設(shè)置此屬性后setSound()會無效//.setDefaults(Notification.DEFAULT_SOUND)//調(diào)用系統(tǒng)多媒體褲內(nèi)的鈴聲//.setSound(Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,"2"));//調(diào)用自己提供的鈴聲,位于 /res/values/raw 目錄下.setSound(Uri.parse("android.resource://com.littlejie.notification/" + R.raw.sound));//另一種設(shè)置鈴聲的方法//Notification notify = builder.build();//調(diào)用系統(tǒng)默認(rèn)鈴聲//notify.defaults = Notification.DEFAULT_SOUND;//調(diào)用自己提供的鈴聲//notify.sound = Uri.parse("android.resource://com.littlejie.notification/"+R.raw.sound);//調(diào)用系統(tǒng)自帶的鈴聲//notify.sound = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,"2");//mManager.notify(2,notify);mManager.notify(2, builder.build()); }/** * 展示有震動效果的通知,需要在AndroidManifest.xml中申請震動權(quán)限 * <uses-permission android:name="android.permission.VIBRATE" /> * 補充:測試震動的時候,手機的模式一定要調(diào)成鈴聲+震動模式,否則你是感受不到震動的 */ private void showNotifyWithVibrate() {//震動也有兩種設(shè)置方法,與設(shè)置鈴聲一樣,在此不再贅述long[] vibrate = new long[]{0, 500, 1000, 1500};NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("我是伴有震動效果的通知").setContentText("顫抖吧,凡人~")//使用系統(tǒng)默認(rèn)的震動參數(shù),會與自定義的沖突//.setDefaults(Notification.DEFAULT_VIBRATE)//自定義震動效果.setVibrate(vibrate);//另一種設(shè)置震動的方法//Notification notify = builder.build();//調(diào)用系統(tǒng)默認(rèn)震動//notify.defaults = Notification.DEFAULT_VIBRATE;//調(diào)用自己設(shè)置的震動//notify.vibrate = vibrate;//mManager.notify(3,notify);mManager.notify(3, builder.build()); }/** * 顯示帶有呼吸燈效果的通知,但是不知道為什么,自己這里測試沒成功 */ private void showNotifyWithLights() {final NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("我是帶有呼吸燈效果的通知").setContentText("一閃一閃亮晶晶~")//ledARGB 表示燈光顏色、 ledOnMS 亮持續(xù)時間、ledOffMS 暗的時間.setLights(0xFF0000, 3000, 3000);Notification notify = builder.build();//只有在設(shè)置了標(biāo)志符Flags為Notification.FLAG_SHOW_LIGHTS的時候,才支持呼吸燈提醒。notify.flags = Notification.FLAG_SHOW_LIGHTS;//設(shè)置lights參數(shù)的另一種方式//notify.ledARGB = 0xFF0000;//notify.ledOnMS = 500;//notify.ledOffMS = 5000;//使用handler延遲發(fā)送通知,因為連接usb時,呼吸燈一直會亮著Handler handler = new Handler();handler.postDelayed(new Runnable() {@Overridepublic void run() {mManager.notify(4, builder.build());}}, 10000); }/** * 顯示帶有默認(rèn)鈴聲、震動、呼吸燈效果的通知 * 如需實現(xiàn)自定義效果,請參考前面三個例子 */ private void showNotifyWithMixed() {NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("我是有鈴聲+震動+呼吸燈效果的通知").setContentText("我是最棒的~")//等價于setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);.setDefaults(Notification.DEFAULT_ALL);mManager.notify(5, builder.build()); }/** * 通知無限循環(huán),直到用戶取消或者打開通知欄(其實觸摸就可以了),效果與FLAG_ONLY_ALERT_ONCE相反 * 注:這里沒有給Notification設(shè)置PendingIntent,也就是說該通知無法響應(yīng),所以只能手動取消 */ private void showInsistentNotify() {NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("我是一個死循環(huán),除非你取消或者響應(yīng)").setContentText("啦啦啦~").setDefaults(Notification.DEFAULT_ALL);Notification notify = builder.build();notify.flags |= Notification.FLAG_INSISTENT;mManager.notify(6, notify); }/** * 通知只執(zhí)行一次,與默認(rèn)的效果一樣 */ private void showAlertOnceNotify() {NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("仔細看,我就執(zhí)行一遍").setContentText("好了,已經(jīng)一遍了~").setDefaults(Notification.DEFAULT_ALL);Notification notify = builder.build();notify.flags |= Notification.FLAG_ONLY_ALERT_ONCE;mManager.notify(7, notify); }/** * 清除所有通知 */ private void clearNotify() {mManager.cancelAll(); }

    至此, Notification 的基本操作都已經(jīng)講完了,發(fā)送一個帶有自定義效果的簡單通知已經(jīng)不在話下。接下去,我們會講 Notification 的一些高級操作。快上車,沒時間解釋了~

    補充:當(dāng)用戶點擊通知欄的時候,正在進行的通知,比如 Notification 的 flags = Notification.FLAG_INSISTENT ,那么相當(dāng)于用戶響應(yīng)了該通知,后面的 Demo 中會給出具體的代碼

    文中如有紕漏,歡迎大家留言指出。
    微博:厲圣杰
    源碼:AndroidDemo/Notification

    參考

  • Android Patterns——Notification
  • Android User Interface——Notification
  • 全面了解Android Notification
  • 作者:登天路

    轉(zhuǎn)載請說明出處:http://www.cnblogs.com/travellife/
    GitPages:http://www.littlejie.com/

    總結(jié)

    以上是生活随笔為你收集整理的Android Notification 详解——基本操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。