歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android中通知的使用-----Notification詳解

Android中通知的使用-----Notification詳解

日期:2017/3/1 10:40:33   编辑:Linux編程
Notification —— 通知,是一種讓你的應用程序在不使用Activity的情況下警示用戶。它是看不見的程序組件(Broadcast Receiver,Service和不活躍的Activity)警示用戶有需要注意的事件發生的最好途徑。 Notification 是由NotificationManager(系統服務)統一管理的。

一般來說, 一個Notification應該傳送的消息包括:

1 、一個狀態條圖標

2、在拉伸的狀態欄窗口中顯示額外的信息和啟動一個Application的Intent

3、閃燈或LED

4、電話震動

在狀態欄(Status Bar)中,通知主要有兩類(使用FLAG_標記,後面講解到):

1、正在運行的事件

2、通知事件

Notification圖解如下:

Notification類介紹:

常量

//表示發送一個Notification的所攜帶的效果

DEFAULT_ALL 使用默認字段

DEFAULT_LIGHTS 默認閃光

DEFAULT_SOUND 默認聲音(uri,指向路徑)

DEFAULT_VIRATE 默認震動

PS:以上的效果常量可以累加,即通過mNotifaction.defaults |=DEFAULT_SOUND (有些效果只能在真機上才有,比如震動)

//設置Flag位

FLAG_AUTO_CANCEL 該通知能被狀態欄的清除按鈕給清除掉

FLAG_NO_CLEAR 該通知能被狀態欄的清除按鈕給清除掉

FLAG_ONGOING_EVENT 通知放置在正在運行

常用字段:

contentIntent 設置PendingIntent對象,點擊時發送該Intent

flags 設置flag位,例如FLAG_NO_CLEAR等

defaults 添加效果

tickerText 顯示在狀態欄中的文字

when 發送此通知的時間戳

icon 設置圖標

常用方法介紹

void setLatestEventInfo(Context context , CharSequence contentTitle,CharSequence contentText,PendingIntent contentIntent)

功能: 顯示在拉伸狀態欄中的Notification屬性,點擊後將發送PendingIntent對象

參數: context 上下文環境

contentTitle 狀態欄中的大標題

contentText 狀態欄中的小標題

contentIntent 點擊後將發送PendingIntent對象

另外的就是Notification的幾步不同構造方法了,其構造方法的參數含義如上,請參考SDK 。

注意:當我們創造了一個Notification對象時,一定要為其設置setLatestEventInfo()方法,否則程序會報錯 .

前面我們說過,NotificationManager是所有Notification的大管家,它的主要職責是加入/移除Notification。

NotificationManager類

通過獲取系統服務來獲取該對象:

NotificationManager mNotificationManager = (NotificationManager)getSystemServic(Context.NOTIFICATION_SERVICE) ;

常用方法:

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

Demo如下:

簡單的寫一個Notification的類,對通知上面所講解的知識有一定的認知 。

  1. package com.qin.notification;
  2. import Android.app.Activity;
  3. import android.app.Notification;
  4. import android.app.NotificationManager;
  5. import android.app.PendingIntent;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12. import android.widget.Button;
  13. public class MainActivity extends Activity implements OnClickListener
  14. {
  15. private Button btnNotify;
  16. private Button btnCancel;
  17. private static int NOTIFICATION_ID = 0 ;
  18. /** Called when the activity is first created. */
  19. @Override
  20. public void onCreate(Bundle savedInstanceState)
  21. {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.main);
  24. btnNotify = (Button) findViewById(R.id.btnNotify);
  25. btnCancel = (Button) findViewById(R.id.btnCancel);
  26. btnNotify.setOnClickListener(this) ;
  27. btnCancel.setOnClickListener(this) ;
  28. }
  29. @Override
  30. public void onClick(View v)
  31. {
  32. switch (v.getId())
  33. {
  34. case R.id.btnNotify:
  35. showNotification();
  36. break;
  37. case R.id.btnCancel:
  38. removeNotification();
  39. break;
  40. default:
  41. break;
  42. }
  43. }
  44. // 顯示一個通知
  45. private void showNotification()
  46. {
  47. // 創建一個通知
  48. Notification mNotification = new Notification();
  49. // 設置屬性值
  50. mNotification.icon = R.drawable.icon;
  51. mNotification.tickerText = "NotificationTest";
  52. mNotification.when = System.currentTimeMillis(); // 立即發生此通知
  53. // 帶參數的構造函數,屬性值如上
  54. // Notification mNotification = = new Notification(R.drawable.icon,"NotificationTest", System.currentTimeMillis()));
  55. // 添加聲音效果
  56. mNotification.defaults |= Notification.DEFAULT_SOUND;
  57. // 添加震動,由於在我的真機上會App發生異常,估計是Android2.2裡的錯誤,略去,不添加
  58. // mNotification.defaults |= Notification.DEFAULT_VIBRATE ;
  59. //添加狀態標志
  60. //FLAG_AUTO_CANCEL 該通知能被狀態欄的清除按鈕給清除掉
  61. //FLAG_NO_CLEAR 該通知能被狀態欄的清除按鈕給清除掉
  62. //FLAG_ONGOING_EVENT 通知放置在正在運行
  63. //FLAG_INSISTENT 通知的音樂效果一直播放
  64. mNotification.flags = Notification.FLAG_INSISTENT ;
  65. // 設置setLatestEventInfo方法,如果不設置會App報錯異常
  66. NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  67. //注冊此通知
  68. // 如果該NOTIFICATION_ID的通知已存在,不會重復添加,只是播放相應的效果(聲音等)
  69. mNotificationManager.notify(NOTIFICATION_ID, mNotification);
  70. }
  71. private void removeNotification()
  72. {
  73. NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  74. // 取消的只是當前Context的Notification
  75. mNotificationManager.cancel(NOTIFICATION_ID);
  76. }
  77. }
  78. }
Copyright © Linux教程網 All Rights Reserved