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


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.             
  2.   
  3. package com.qin.notification;  
  4.   
  5. import Android.app.Activity;  
  6. import android.app.Notification;  
  7. import android.app.NotificationManager;  
  8. import android.app.PendingIntent;  
  9. import android.content.Context;  
  10. import android.content.Intent;  
  11. import android.os.Bundle;  
  12. import android.util.Log;  
  13. import android.view.View;  
  14. import android.view.View.OnClickListener;  
  15. import android.widget.Button;  
  16.   
  17. public class MainActivity extends Activity implements OnClickListener  
  18. {   
  19.      private Button btnNotify;  
  20.      private Button btnCancel;  
  21.   
  22.      private static int NOTIFICATION_ID = 0 ;  
  23.   
  24.       /** Called when the activity is first created. */  
  25.     @Override  
  26.     public void onCreate(Bundle savedInstanceState)  
  27.     {  
  28.         super.onCreate(savedInstanceState);  
  29.         setContentView(R.layout.main);  
  30.         btnNotify = (Button) findViewById(R.id.btnNotify);  
  31.         btnCancel = (Button) findViewById(R.id.btnCancel);  
  32.         btnNotify.setOnClickListener(this) ;  
  33.         btnCancel.setOnClickListener(this) ;  
  34.    }  
  35.   @Override  
  36.   public void onClick(View v)  
  37.   {  
  38.       switch (v.getId())  
  39.         {  
  40.          case R.id.btnNotify:  
  41.              showNotification();  
  42.              break;  
  43.          case R.id.btnCancel:  
  44.              removeNotification();  
  45.              break;  
  46.          default:  
  47.                 break;  
  48.   
  49.        }  
  50.   }  
  51.     // 显示一个通知   
  52.     private void showNotification()  
  53.     {  
  54.       // 创建一个通知   
  55.       Notification mNotification = new Notification();  
  56.   
  57.       // 设置属性值   
  58.        mNotification.icon = R.drawable.icon;  
  59.       mNotification.tickerText = "NotificationTest";  
  60.       mNotification.when = System.currentTimeMillis(); // 立即发生此通知   
  61.   
  62.        // 带参数的构造函数,属性值如上   
  63.        // Notification mNotification = = new Notification(R.drawable.icon,"NotificationTest", System.currentTimeMillis()));   
  64.   
  65.       // 添加声音效果   
  66.         mNotification.defaults |= Notification.DEFAULT_SOUND;  
  67.   
  68.        // 添加震动,由于在我的真机上会App发生异常,估计是Android2.2里的错误,略去,不添加   
  69.         // mNotification.defaults |= Notification.DEFAULT_VIBRATE ;    
  70.   
  71.        //添加状态标志    
  72.   
  73.         //FLAG_AUTO_CANCEL          该通知能被状态栏的清除按钮给清除掉   
  74.         //FLAG_NO_CLEAR                 该通知能被状态栏的清除按钮给清除掉   
  75.         //FLAG_ONGOING_EVENT      通知放置在正在运行   
  76.         //FLAG_INSISTENT                通知的音乐效果一直播放   
  77.         mNotification.flags = Notification.FLAG_INSISTENT ;  
  78.   
  79.       // 设置setLatestEventInfo方法,如果不设置会App报错异常   
  80.        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
  81.       //注册此通知    
  82.        // 如果该NOTIFICATION_ID的通知已存在,不会重复添加,只是播放相应的效果(声音等)   
  83.   
  84.       mNotificationManager.notify(NOTIFICATION_ID, mNotification);  
  85.   
  86.   }  
  87.   
  88.      
  89.   
  90.    private void removeNotification()  
  91.   
  92.    {  
  93.   
  94.        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
  95.   
  96.        // 取消的只是当前Context的Notification   
  97.   
  98.       mNotificationManager.cancel(NOTIFICATION_ID);  
  99.    }  
  100.   
  101.  }  
  102.   
  103. }  

相关内容