Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 資訊

Android Notification 通知機制詳解

編輯:Android 資訊

根據 Activity 的生命週期,當 Activity 不顯示時(比如按下 Home 鍵),會執行 onStop 函數。所以你可以在 onStop 函數(按離開鍵除外)裡把 notification 放進通知欄,等再次顯示時再把它從通知欄移除;或者,只要程式還在運行,就一直顯示通知欄圖示。

下面簡單介紹 Notification 類中的一些常量、欄位與方法:

常量:

DEFAULT_ALL 使用所有預設值,比如聲音、震動、閃屏等等
DEFAULT_LIGHTS 使用預設閃光提示
DEFAULT_SOUNDS 使用預設提示聲音
DEFAULT_VIBRATE 使用預設手機震動
【說明】:若要用到手機震動,一定要在 manifest.xml 裡加入權限:

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

以上效果常量可以疊加,也就是透過

notification.defaults = DEFAULT_SOUND | DEFAULT_VIBRATE;
notification.defaults |= DEFAULT_SOUND (最好在真機上測試,模擬器上沒有震動效果)

//設定 flag 位
FLAG_AUTO_CANCEL 該通知能被狀態欄的清除按鈕清除掉
FLAG_NO_CLEAR 該通知不能被狀態欄的清除按鈕清除掉
FLAG_ONGOING_EVENT 通知放置在「正在運行」
FLAG_INSISTENT 是否一直進行,比如音樂一直播放,直到使用者回應

常用欄位:

contentIntent 設定 PendingIntent 物件,點擊時發送該 Intent
defaults 添加預設效果
flags 設定 flag 位,例如 FLAG_NO_CLEAR 等
icon 設定圖示
sound 設定聲音
tickerText 顯示在狀態欄中的文字
when 發送此通知的時間戳

NotificationManager 常用方法介紹:

public void cancelAll() 移除所有通知(只針對當前 Context 下的 Notification)
public void cancel(int id) 移除標記為 id 的通知(只針對當前 Context 下的所有 Notification)
public void notify(String tag, int id, Notification notification) 將通知加入狀態欄,標籤為 tag、標記為 id
public void notify(int id, Notification notification) 將通知加入狀態欄,標記為 id

package com.ljq.activity; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); clearNotification(); } @Override protected void onStop() { showNotification(); super.onStop(); } @Override protected void onStart() { clearNotification(); super.onStart(); } /** * 在狀態欄顯示通知 */ private void showNotification(){ // 創建一個NotificationManager的引用 NotificationManager notificationManager = (NotificationManager) this.getSystemService(android.content.Context.NOTIFICATION_SERVICE); // 定義Notification的各種屬性 Notification notification =new Notification(R.drawable.icon, "督導系統", System.currentTimeMillis()); //FLAG_AUTO_CANCEL 該通知能被狀態欄的清除按鈕給清除掉 //FLAG_NO_CLEAR 該通知不能被狀態欄的清除按鈕給清除掉 //FLAG_ONGOING_EVENT 通知放置在正在運行 //FLAG_INSISTENT 是否一直進行,比如音樂一直播放,知道用戶響應 notification.flags |= Notification.FLAG_ONGOING_EVENT; // 將此通知放到通知欄的"Ongoing"即"正在運行"組中 notification.flags |= Notification.FLAG_NO_CLEAR; // 表明在點擊了通知欄中的"清除通知"後,此通知不清除,經常與FLAG_ONGOING_EVENT一起使用 notification.flags |= Notification.FLAG_SHOW_LIGHTS; //DEFAULT_ALL 使用所有默認值,比如聲音,震動,閃屏等等 //DEFAULT_LIGHTS 使用默認閃光提示 //DEFAULT_SOUNDS 使用默認提示聲音 //DEFAULT_VIBRATE 使用默認手機震動,需加上<uses-permission android:name="android.permission.VIBRATE" />權限 notification.defaults = Notification.DEFAULT_LIGHTS; //疊加效果常量 //notification.defaults=Notification.DEFAULT_LIGHTS|Notification.DEFAULT_SOUND; notification.ledARGB = Color.BLUE; notification.ledOnMS =5000; //閃光時間,毫秒 // 設置通知的事件消息 CharSequence contentTitle ="督導系統標題"; // 通知欄標題 CharSequence contentText ="督導系統內容"; // 通知欄內容 Intent notificationIntent =new Intent(MainActivity.this, MainActivity.class); // 點擊該通知後要跳轉的Activity PendingIntent contentItent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, contentTitle, contentText, contentItent); // 把Notification傳遞給NotificationManager notificationManager.notify(0, notification); }
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved