Android 狀態列通知Notification、NotificationManager簡介

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   ar   color   os   sp   

  Notification(通知)一般用在電話,簡訊,郵件,鬧鐘鈴聲,在手機的狀態列上就會出現一個小表徵圖,提示使用者處理這個通知,這時手從上方滑動狀態列就可以展開並處理這個通知;

  在Android系統中,發一個狀態列通知還是很方便的。發送一個狀態列通知必須用到兩個類:  NotificationManager 、 Notification;

  NotificationManager :  是狀態列通知的管理類,負責發通知、清楚通知等;NotificationManager 是一個系統Service,必須通過 getSystemService()方法來擷取;

  Notification:是具體的狀態列通知對象,可以設定icon、文字、提示聲音、震動等等參數;

 

我們先看下一個通知需要的基本參數:

(1)Icon:表徵圖;

(2)PendingIntent:點擊通知執行頁面跳轉;

(2)Ticker Text:Notification剛出來的時候,在狀態列上滾動的字幕,如果很長,會自動分割滾動;

(3)Content Title:Notification展開後的標題;
(4)Content Text:Notification展開後的內容;

 

而一個通知通常需要以下幾步:

(1)擷取NotificationManager:

nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

 

(2)執行個體化Notification對象,並設定Notification的屬性;

notification.icon = R.drawable.icon;  // 設定通知在狀態列顯示的表徵圖                notification.tickerText = "One Message is coming!!!";  // 通知在狀態列顯示的內容notification.when = when;    // 設定來通知時的時間notification.sound = Uri.parse("android.resource://com.sun.alex/raw/dida"); // 自訂聲音notification.flags |= Notification.FLAG_AUTO_CANCEL;  // 點擊清除按鈕或點擊通知後會自動消失  notification.flags |= Notification.FLAG_INSISTENT;   // 一直進行,比如音樂一直播放,知道使用者響應  notification.defaults = Notification.DEFAULT_SOUND;   // 調用系統內建聲音  notification.defaults = Notification.DEFAULT_VIBRATE;  // 設定預設震動  notification.defaults = Notification.DEFAULT_ALL;   // 設定鈴聲震動  notification.defaults = Notification.DEFAULT_ALL;   // 把所有的屬性設定成預設  

 

(3)調用setLatestEventInfo()方法在視圖中設定表徵圖和時間;

// 執行個體化IntentIntent intent = new Intent(MainActivity.this, MainActivity.class);// 獲得PendingIntentPendingIntent pIntent = PendingIntent.getActivity(MainActivity.this, 0,intent, 0);// 設定事件資訊notification.setLatestEventInfo(MainActivity.this, " Title", "Content",pIntent);

 

(4)發出通知:通過NotificationManager對象的notify()方法來執行一個Notification的訊息;

private int Notification_ID = 110;nManager.notify(Notification_ID, notification);

 

(5)清除通知:通過NotificationManager的cancel(int)方法,來清除某個通知,其中參數是Notification的唯一標識ID,當然也可以通過cancelAll()來清除狀態列所有的通知;

nManager.cancel(Notification_ID);

 

下面用例子來說明一下:

三個按鈕,分別是發送,然後更新,最後清除通知;

第一步,點擊發送後;

 

然後查看通知:

第二步,點擊更新通知,然後查看通知:

 

 

第三步:清除通知:

 

 

實現代碼如下:

布局檔案activity_main.xml 只有三個按鈕,就不寫了;

主要看MainActivity.java

package com.xiaozhang.notificationtest;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {    private Button button1, button2, button3;    // 通知管理器    private NotificationManager nManager;    // 通知顯示內容    private PendingIntent pendingIntent;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        init();    }    private void init() {        button1 = (Button) findViewById(R.id.button1);        button2 = (Button) findViewById(R.id.button2);        button3 = (Button) findViewById(R.id.button3);        button1.setOnClickListener(onclick);        button2.setOnClickListener(onclick);        button3.setOnClickListener(onclick);        nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);        Intent intent = new Intent(this, MainActivity.class);        pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent,                0);    }    OnClickListener onclick = new OnClickListener() {        private int Notification_ID = 110;        private Notification notification;        @SuppressWarnings("deprecation")        @Override        public void onClick(View v) {            switch (v.getId()) {            case R.id.button1:                notification = new Notification();                notification.icon = R.drawable.icon; // 設定通知在狀態列顯示的表徵圖                notification.tickerText = "One Message is coming!!!"; // 通知在狀態列顯示的內容                // 如果要全部採用預設值, 用 DEFAULT_ALL.                // 此處採用預設聲音                notification.defaults |= Notification.DEFAULT_SOUND;                notification.defaults |= Notification.DEFAULT_VIBRATE;                notification.defaults |= Notification.DEFAULT_LIGHTS;                // 讓聲音、震動無限迴圈,直到使用者響應                notification.flags |= Notification.FLAG_INSISTENT;                // 通知被點擊後,自動消失                notification.flags |= Notification.FLAG_AUTO_CANCEL;                // 第二個參數 :下拉狀態列時顯示的訊息標題                // 第三個參數:下拉狀態列時顯示的訊息內容                // 第四個參數:點擊該通知時執行頁面跳轉                notification.setLatestEventInfo(MainActivity.this, "通知1",                        "第一條資訊:one message", pendingIntent);                // 發出狀態列通知                nManager.notify(Notification_ID, notification);                break;            case R.id.button2:                // 更新通知                // 比如狀態列提示有一條新簡訊,還沒來得及查看,又來一條新簡訊的提示;此時可以採用更新原來通知的方式。                // (再重新發一個通知也可以,但是這樣會造成通知的混亂,而且顯示多個通知給使用者,對使用者也不友好)                notification.setLatestEventInfo(MainActivity.this, "通知2",                        "第二條訊息:second message", pendingIntent);                nManager.notify(Notification_ID, notification);                break;            case R.id.button3:                nManager.cancel(Notification_ID);                break;            }        }    };}

 

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.