Android 状态栏通知Notification、NotificationManager详解

本文转自:http://blog.csdn.net/qq_31307919/article/details/51538391

Android系统中,发一个状态栏通知还是很方便的。下面我们就来看一下,怎么发送状态栏通知,状态栏通知又有哪些参数可以设置?

 

首先,发送一个状态栏通知必须用到两个类:  NotificationManager 、 Notification。

 

NotificationManager :  是状态栏通知的管理类,负责发通知、清楚通知等。

NotificationManager 是一个系统Service,必须通过 getSystemService()方法来获取。

 

1 <code>NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);</code>

 

Notification:是具体的状态栏通知对象,可以设置icon、文字、提示声音、振动等等参数。

 

下面是设置一个通知需要的基本参数:

 

  • An icon  (通知的图标)
  • A title and expanded message  (通知的标题和内容)
  • PendingIntent   (点击通知执行页面跳转)

 

可选的设置:

 

  • A ticker-text message (状态栏顶部提示消息)
  • An alert sound    (提示音)
  • A vibrate setting  (振动)
  • A flashing LED setting  (灯光)
  • 等等

 

一、创建Notification

通过NotificationManager  的 notify(int, Notification) 方法来启动Notification。

   第一个参数唯一的标识该Notification,第二个参数就是Notification对象。

二、更新Notification

调用Notification的 setLatestEventInfo方法来更新内容,然后再调用NotificationManager的notify()方法即可。(具体可以看下面的实例)

 

三、删除Notification

通过NotificationManager  的cancel(int)方法,来清除某个通知。其中参数就是Notification的唯一标识ID。

当然也可以通过  cancelAll() 来清除状态栏所有的通知。

 

四、Notification设置(振动、铃声等)

 

1.  基本设置:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //新建状态栏通知  
  2. baseNF = new Notification();  
  3. //设置通知在状态栏显示的图标  
  4. baseNF.icon = R.drawable.icon;  
  5. //通知时在状态栏显示的内容  
  6. baseNF.tickerText = "You clicked BaseNF!";  
  7. //通知的默认参数 DEFAULT_SOUND, DEFAULT_VIBRATE, DEFAULT_LIGHTS.  
  8. //如果要全部采用默认值, 用 DEFAULT_ALL.  
  9. //此处采用默认声音  
  10. baseNF.defaults = Notification.DEFAULT_SOUND;  
  11. //第二个参数 :下拉状态栏时显示的消息标题 expanded message title  
  12. //第三个参数:下拉状态栏时显示的消息内容 expanded message text  
  13. //第四个参数:点击该通知时执行页面跳转  
  14. baseNF.setLatestEventInfo(Lesson_10.this"Title01""Content01", pd);  
  15. //发出状态栏通知  
  16. //The first parameter is the unique ID for the Notification  
  17. // and the second is the Notification object.  
  18. nm.notify(Notification_ID_BASE, baseNF);  

  也可以封装成一个方法
    1. publicvoid showNotification(int icon,String tickertext,String title,String content){
    2. //设置一个唯一的ID,随便设置
    3. //Notification管理器
    4. Notification notification=newNotification(icon,tickertext,System.currentTimeMillis());
    5. //后面的参数分别是显示在顶部通知栏的小图标,小图标旁的文字(短暂显示,自动消失)系统当前时间(不明白这个有什么用)
    6. notification.defaults=Notification.DEFAULT_ALL;
    7. //这是设置通知是否同时播放声音或振动,声音为Notification.DEFAULT_SOUND
    8. //振动为Notification.DEFAULT_VIBRATE;
    9. //Light为Notification.DEFAULT_LIGHTS,在我的Milestone上好像没什么反应
    10. //全部为Notification.DEFAULT_ALL
    11. //如果是振动或者全部,必须在AndroidManifest.xml加入振动权限
    12. PendingIntent pt=PendingIntent.getActivity(this,0,newIntent(this,main.class),0);
    13. //点击通知后的动作,这里是转回main 这个Acticity
    14. notification.setLatestEventInfo(this,title,content,pt);
    15. nm.notify(notification_id, notification);
    16. }


配一张图作说明:

2. 添加声音

如果要采用默认声音,只要使用default就可以了。

1 baseNF.defaults = Notification.DEFAULT_SOUND;

如果要使用自定义声音,那么就要用到sound了。如下:

1 notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");

上面这种方法,使用的是自己的铃声,如果想用系统自带的铃声,可以这样:

1 notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");

需要注意一点,如果default、sound同时出现,那么sound无效,会使用默认铃声。

 

   默认情况下,通知的声音播放一遍就会结束。 如果你想让声音循环播放,需要为flags参数加上FLAG_INSISTENT。 这样声音会到用户响应才结束,比如下拉状态栏。

1 notification.flags |= notification.FLAG_INSISTENT;

3. 添加振动

如果是使用默认的振动方式,那么同样也是使用default。

1 notification.defaults |= Notification.DEFAULT_VIBRATE;

当然也可以自己定义振动形式,这边需要用到Long型数组。

1 long[] vibrate = {0,100,200,300};
2 notification.vibrate = vibrate;

这边的Long型数组中,第一个参数是开始振动前等待的时间,第二个参数是第一次振动的时间,第三个参数是第二次振动的时间,以此类推,随便定义多长的数组。但是采用这种方法,没有办法做到重复振动。

 

同样,如果default、vibrate同时出现时,会采用默认形式。

 

另外还需要注意一点:使用振动器时需要权限,如下:

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

4. 闪光

 使用默认的灯光,如下:

1 notification.defaults |= Notification.DEFAULT_LIGHTS;

自定义

1 notification.ledARGB = 0xff00ff00;
2 notification.ledOnMS = 300;
3 notification.ledOffMS = 1000;
4 notification.flags |= Notification.FLAG_SHOW_LIGHTS;

其中ledARGB 表示灯光颜色、 ledOnMS 亮持续时间、ledOffMS 暗的时间。

注意:这边的颜色跟设备有关,不是所有的颜色都可以,要看具体设备。

 

5. 其他有用的设置:

flags

Notification.FLAG_INSISTENT;  //让声音、振动无限循环,直到用户响应

Notification.FLAG_AUTO_CANCEL;    //通知被点击后,自动消失

Notification.FLAG_NO_CLEAR; //点击'Clear'时,不清楚该通知(QQ的通知无法清除,就是用的这个)


全部代码如下所示:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.yfz;  
  2. import android.app.Activity;  
  3. import android.app.Notification;  
  4. import android.app.NotificationManager;  
  5. import android.app.PendingIntent;  
  6. import android.content.Intent;  
  7. import android.net.Uri;  
  8. import android.os.Bundle;  
  9. import android.provider.MediaStore.Audio;  
  10. import android.util.Log;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.Button;  
  14. import android.widget.RemoteViews;  
  15. import android.widget.SeekBar;  
  16. import android.widget.TextView;  
  17. /** 
  18.  * Notification 
  19.  * @author  Administrator 
  20.  * 
  21.  */  
  22. public class Lesson_10 extends Activity {  
  23.        
  24.     //BaseNotification  
  25.     private Button bt01;  
  26.     //UpdateBaseNotification  
  27.     private Button bt02;  
  28.        
  29.     //ClearBaseNotification  
  30.     private Button bt03;  
  31.        
  32.     //MediaNotification  
  33.     private Button bt04;  
  34.        
  35.     //ClearMediaNotification  
  36.     private Button bt05;  
  37.        
  38.     //ClearALL  
  39.     private Button bt06;  
  40.        
  41.     //CustomNotification  
  42.     private Button bt07;  
  43.        
  44.     //通知管理器  
  45.     private NotificationManager nm;  
  46.        
  47.     //通知显示内容  
  48.     private PendingIntent pd;  
  49.        
  50.     @Override  
  51.      public void onCreate(Bundle savedInstanceState) {  
  52.             super.onCreate(savedInstanceState);  
  53.             /*加载页面*/  
  54.             setContentView(R.layout.lesson10);  
  55.                
  56.             init();  
  57.     }  
  58.        
  59.     private void init() {  
  60.         bt01 = (Button)findViewById(R.id.le10bt01);  
  61.         bt02 = (Button)findViewById(R.id.le10bt02);  
  62.         bt03 = (Button)findViewById(R.id.le10bt03);  
  63.         bt04 = (Button)findViewById(R.id.le10bt04);  
  64.         bt05 = (Button)findViewById(R.id.le10bt05);  
  65.         bt06 = (Button)findViewById(R.id.le10bt06);  
  66.         bt07 = (Button)findViewById(R.id.le10bt07);  
  67.            
  68.         bt01.setOnClickListener(onclick);  
  69.         bt02.setOnClickListener(onclick);  
  70.         bt03.setOnClickListener(onclick);  
  71.         bt04.setOnClickListener(onclick);  
  72.         bt05.setOnClickListener(onclick);  
  73.         bt06.setOnClickListener(onclick);    
  74.         bt07.setOnClickListener(onclick);  
  75.            
  76.         nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
  77.            
  78.         Intent intent = new Intent(this,Lesson_10.class);  
  79.            
  80.         pd = PendingIntent.getActivity(Lesson_10.this0, intent, 0);  
  81.     }  
  82.        
  83.     OnClickListener onclick = new OnClickListener() {  
  84.            
  85.         //BASE Notification ID  
  86.         private int Notification_ID_BASE = 110;  
  87.            
  88.         private Notification baseNF;  
  89.            
  90.         //Notification ID  
  91.         private int Notification_ID_MEDIA = 119;  
  92.            
  93.         private Notification mediaNF;  
  94.            
  95.         @Override  
  96.         public void onClick(View v) {  
  97.             switch(v.getId()) {  
  98.                 case R.id.le10bt01:  
  99.                     //新建状态栏通知  
  100.                     baseNF = new Notification();  
  101.                         
  102.                     //设置通知在状态栏显示的图标  
  103.                     baseNF.icon = R.drawable.icon;  
  104.                        
  105.                     //通知时在状态栏显示的内容  
  106.                     baseNF.tickerText = "You clicked BaseNF!";  
  107.                        
  108.                     //通知的默认参数 DEFAULT_SOUND, DEFAULT_VIBRATE, DEFAULT_LIGHTS.  
  109.                     //如果要全部采用默认值, 用 DEFAULT_ALL.  
  110.                     //此处采用默认声音  
  111.                     baseNF.defaults |= Notification.DEFAULT_SOUND;  
  112.                     baseNF.defaults |= Notification.DEFAULT_VIBRATE;  
  113.                     baseNF.defaults |= Notification.DEFAULT_LIGHTS;  
  114.                        
  115.                     //让声音、振动无限循环,直到用户响应  
  116.                     baseNF.flags |= Notification.FLAG_INSISTENT;  
  117.                        
  118.                     //通知被点击后,自动消失  
  119.                     baseNF.flags |= Notification.FLAG_AUTO_CANCEL;  
  120.                        
  121.                     //点击'Clear'时,不清楚该通知(QQ的通知无法清除,就是用的这个)  
  122.                     baseNF.flags |= Notification.FLAG_NO_CLEAR;  
  123.                        
  124.                        
  125.                     //第二个参数 :下拉状态栏时显示的消息标题 expanded message title  
  126.                     //第三个参数:下拉状态栏时显示的消息内容 expanded message text  
  127.                     //第四个参数:点击该通知时执行页面跳转  
  128.                     baseNF.setLatestEventInfo(Lesson_10.this"Title01""Content01", pd);  
  129.                        
  130.                     //发出状态栏通知  
  131.                     //The first parameter is the unique ID for the Notification  
  132.                     // and the second is the Notification object.  
  133.                     nm.notify(Notification_ID_BASE, baseNF);  
  134.                        
  135.                     break;  
  136.                        
  137.                 case R.id.le10bt02:  
  138.                     //更新通知  
  139.                     //比如状态栏提示有一条新短信,还没来得及查看,又来一条新短信的提示。  
  140.                     //此时采用更新原来通知的方式比较。  
  141.                     //(再重新发一个通知也可以,但是这样会造成通知的混乱,而且显示多个通知给用户,对用户也不友好)  
  142.                     baseNF.setLatestEventInfo(Lesson_10.this"Title02""Content02", pd);  
  143.                     nm.notify(Notification_ID_BASE, baseNF);  
  144.                     break;  
  145.                        
  146.                 case R.id.le10bt03:  
  147.                        
  148.                     //清除 baseNF  
  149.                     nm.cancel(Notification_ID_BASE);  
  150.                     break;  
  151.                        
  152.                 case R.id.le10bt04:  
  153.                     mediaNF = new Notification();  
  154.                     mediaNF.icon = R.drawable.icon;  
  155.                     mediaNF.tickerText = "You clicked MediaNF!";  
  156.                        
  157.                     //自定义声音  
  158.                     mediaNF.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");  
  159.                        
  160.                     //通知时发出的振动  
  161.                     //第一个参数: 振动前等待的时间  
  162.                     //第二个参数: 第一次振动的时长、以此类推  
  163.                     long[] vir = {0,100,200,300};  
  164.                     mediaNF.vibrate = vir;  
  165.                        
  166.                     mediaNF.setLatestEventInfo(Lesson_10.this"Title03""Content03", pd);  
  167.                        
  168.                     nm.notify(Notification_ID_MEDIA, mediaNF);  
  169.                     break;  
  170.                        
  171.                 case R.id.le10bt05:  
  172.                     //清除 mediaNF  
  173.                     nm.cancel(Notification_ID_MEDIA);  
  174.                     break;  
  175.                        
  176.                 case R.id.le10bt06:  
  177.                     nm.cancelAll();  
  178.                     break;  
  179.                        
  180.                 case R.id.le10bt07:  
  181.                     //自定义下拉视图,比如下载软件时,显示的进度条。  
  182.                     Notification notification = new Notification();  
  183.                        
  184.                     notification.icon = R.drawable.icon;  
  185.                     notification.tickerText = "Custom!";  
  186.                        
  187.                     RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom);  
  188.                     contentView.setImageViewResource(R.id.image, R.drawable.icon);  
  189.                     contentView.setTextViewText(R.id.text, "Hello, this message is in a custom expanded view");  
  190.                     notification.contentView = contentView;  
  191.                        
  192.                     //使用自定义下拉视图时,不需要再调用setLatestEventInfo()方法  
  193.                     //但是必须定义 contentIntent  
  194.                     notification.contentIntent = pd;  
  195.                        
  196.                     nm.notify(3, notification);  
  197.                     break;  
  198.             }  
  199.         }  
  200.     };  
  201.        
  202. }  

主页面:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:layout_width="fill_parent"  
  5.   android:layout_height="fill_parent"  
  6.   android:orientation="vertical">  
  7.     <Button  
  8.         android:id="@+id/le10bt01"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="BaseNotification"  
  12.     />  
  13.     <Button  
  14.         android:id="@+id/le10bt02"  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="UpdateBaseNotification"  
  18.     />  
  19.     <Button  
  20.         android:id="@+id/le10bt03"  
  21.         android:layout_width="fill_parent"  
  22.         android:layout_height="wrap_content"  
  23.         android:text="ClearBaseNotification"  
  24.     />  
  25.     <Button  
  26.         android:id="@+id/le10bt04"  
  27.         android:layout_width="fill_parent"  
  28.         android:layout_height="wrap_content"  
  29.         android:text="MediaNotification"  
  30.     />  
  31.     <Button  
  32.         android:id="@+id/le10bt05"  
  33.         android:layout_width="fill_parent"  
  34.         android:layout_height="wrap_content"  
  35.         android:text="ClearMediaNotification"  
  36.     />  
  37.     <Button  
  38.         android:id="@+id/le10bt06"  
  39.         android:layout_width="fill_parent"  
  40.         android:layout_height="wrap_content"  
  41.         android:text="ClearALL"  
  42.     />  
  43.     <Button  
  44.         android:id="@+id/le10bt07"  
  45.         android:layout_width="fill_parent"  
  46.         android:layout_height="wrap_content"  
  47.         android:text="CustomNotification"  
  48.     />  
  49. </LinearLayout>  

自定义视图页面:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.               android:orientation="horizontal"  
  4.               android:layout_width="fill_parent"  
  5.               android:layout_height="fill_parent"  
  6.               android:padding="3dp"  
  7.               >  
  8.     <ImageView android:id="@+id/image"  
  9.               android:layout_width="wrap_content"  
  10.               android:layout_height="fill_parent"  
  11.               android:layout_marginRight="10dp"  
  12.               />  
  13.     <TextView android:id="@+id/text"  
  14.               android:layout_width="wrap_content"  
  15.               android:layout_height="fill_parent"  
  16.               android:textColor="#000"  
  17.               />  
  18. </LinearLayout>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值