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

Notification —— 通知,是一种让你的应用程序在不使用Activity的情况下警示用户。它是看不见的程序组件(Broadcast Receiver,Service和不活跃的Activity)警示用户有需要注意的事件发生的最好途径。 Notification 是由NotificationManager(系统服务)统一管理的。

一般来说,一个Notification应该传送的消息包括:

1 、一个状态条图标

2、在拉伸的状态栏窗口中显示额外的信息和启动一个Application的Intent

3、闪灯或LED

4、电话震动

在状态栏(Status Bar)中,通知主要有两类(使用FLAG_标记,后面讲解到):

1、正在运行的事件

2、通知事件

Notification图解如下:

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

Notification类介绍:

常量

//表示发送一个Notification的所携带的效果

DEFAULT_ALL 使用默认字段

DEFAULT_LIGHTS 默认闪光

DEFAULT_SOUND 默认声音(uri,指向路径)

DEFAULT_VIRATE 默认震动,后来得知需要添加震动权限VIBRATE: android.permission.VIBRATE

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

//设置Flag位

FLAG_AUTO_CANCEL该通知能被状态栏的清除按钮给清除掉

FLAG_NO_CLEAR该通知不能被状态栏的清除按钮给清除掉

FLAG_ONGOING_EVENT通知放置在正在运行

常用字段

contentView通知在状态栏的显示View(自定义,具体请看下文) ,常与contentIntent配合使用,点击该通知后,

即触发contentIntent

contentIntent设置PendingIntent对象,点击该通知时发送该Intent

flags设置flag位,例如FLAG_NO_CLEAR等

defaults 添加效果

tickerText显示在状态栏中的文字

when发送此通知的时间戳

icon设置图标

常用方法介绍

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

功能: 显示在拉伸状态栏中的Notification属性,点击后将发送PendingIntent对象。

参数: context上下文环境

contentTitle 状态栏中的大标题

contentText 状态栏中的小标题

contentIntent 点击后将发送PendingIntent对象

另外的就是Notification的几步不同构造方法了,其构造方法的参数含义如上,请参考SDK 。

注意,关于通知(Notification)的显示类型有两种:

第一种使用默认的形式(效果图如上显示)。具体使用是为Notification对象设置setLatestEventInfo()方法(该方法内部创建

了默认的RemoteViews对象,因此为默认显示),否则程序会报异常 ;

第二种 使用自定义的View(RemoteViews对象)显示(功能更加*,强大),具体方法为设置Notification对象的

contentView 属性和contentIntent属性 ,此时不需要设置setLatestEventInfo()方法。具体使用方法如下

  1. Notificationnoti=newNotification(icon,title,when+10000);
  2. noti.flags=Notification.FLAG_INSISTENT;
  3. //1、创建一个自定义的消息布局notification.xml
  4. //2、在程序代码中使用RemoteViews的方法来定义image和text。然后把RemoteViews对象传到contentView字段
  5. RemoteViewsremoteView=newRemoteViews(this.getPackageName(),R.layout.notification);
  6. remoteView.setImageViewResource(R.id.image,R.drawable.icon);
  7. remoteView.setTextViewText(R.id.text,"Hello,thismessageisinacustomexpandedview");
  8. noti.contentView=remoteView;
  9. //3、为Notification的contentIntent字段定义一个Intent(注意,使用自定义View不需要setLatestEventInfo()方法)
  10. //这儿点击后简答启动Settings模块
  11. PendingIntentcontentIntent=PendingIntent.getActivity
  12. (MainActivity.this,0,newIntent("android.settings.SETTINGS"),0);
  13. noti.contentIntent=contentIntent;


本文采用的RemoteViews资源文件如下:/layout/notification.xml

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="horizontal"android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <ImageViewandroid:id="@+id/image"android:layout_width="wrap_content"
  6. android:layout_height="fill_parent"/>
  7. <TextViewandroid:id="@+id/text"android:layout_width="wrap_content"
  8. android:layout_toRightOf="@+id/image"
  9. android:layout_height="wrap_content"android:textColor="#000"/>
  10. <ProgressBarandroid:id="@+id/progress_horizontal"
  11. style="?android:attr/progressBarStyleHorizontal"
  12. android:layout_below="@+id/text"
  13. android:layout_toRightOf="@+id/image"
  14. android:layout_width="fill_parent"android:layout_height="wrap_content"
  15. android:max="100"android:progress="50"android:secondaryProgress="75"/>
  16. </RelativeLayout>

效果图如下:

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

前面我们说过,NotificationManager是所有Notification的大管家,它的主要职责是加入/移除Notification。

NotificationManager类

通过获取系统服务来获取该对象:

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

常用方法:

public void cancelAll() 移除所有通知(只是针对当前Context下的Notification)

public voidcancel(int id) 移除标记为id的通知 (只是针对当前Context下的所有Notification)

public voidnotify(String tag ,int id, Notification notification) 将通知加入状态栏, 标签为tag,标记为id

public voidnotify(int id, Notification notification)将通知加入状态栏,,标记为id

Demo如下:

说明:示例Demo演示了创建两种不同类型的Notification , 实现起来也是很简单的。其实说到两种不同类型的使用

方式 ,其实内部原理是差不多的。

PS :自定义Notification复用了文章之前的布局文件,请知晓。

  1. packagecom.feixun.qin;
  2. importandroid.app.Activity;
  3. importandroid.app.Notification;
  4. importandroid.app.NotificationManager;
  5. importandroid.app.PendingIntent;
  6. importandroid.content.BroadcastReceiver;
  7. importandroid.content.Context;
  8. importandroid.content.Intent;
  9. importandroid.content.IntentFilter;
  10. importandroid.net.Uri;
  11. importandroid.os.Bundle;
  12. importandroid.provider.MediaStore.Audio;
  13. importandroid.util.Log;
  14. importandroid.view.View;
  15. importandroid.widget.Button;
  16. importandroid.widget.RemoteViews;
  17. importandroid.widget.RemoteViews.RemoteView;
  18. publicclassMainActivityextendsActivity{
  19. privateButtonsendNotiBt;
  20. privateintcount=0;
  21. /**Calledwhentheactivityisfirstcreated.*/
  22. @Override
  23. publicvoidonCreate(BundlesavedInstanceState){
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.main);
  26. sendNotiBt=(Button)findViewById(R.id.sendNotiBt);
  27. sendNotiBt.setOnClickListener(newView.OnClickListener(){
  28. @Override
  29. publicvoidonClick(Viewv){
  30. //TODOAuto-generatedmethodstub
  31. showDefaultNotification();
  32. }
  33. });
  34. }
  35. //自定义显示的通知,创建RemoteView对象
  36. privatevoidshowCustomizeNotification(){
  37. CharSequencetitle="iamnew";
  38. inticon=R.drawable.icon;
  39. longwhen=System.currentTimeMillis();
  40. Notificationnoti=newNotification(icon,title,when+10000);
  41. noti.flags=Notification.FLAG_INSISTENT;
  42. //1、创建一个自定义的消息布局view.xml
  43. //2、在程序代码中使用RemoteViews的方法来定义image和text。然后把RemoteViews对象传到contentView字段
  44. RemoteViewsremoteView=newRemoteViews(this.getPackageName(),R.layout.notification);
  45. remoteView.setImageViewResource(R.id.image,R.drawable.icon);
  46. remoteView.setTextViewText(R.id.text,"通知类型为:自定义View");
  47. noti.contentView=remoteView;
  48. //3、为Notification的contentIntent字段定义一个Intent(注意,使用自定义View不需要setLatestEventInfo()方法)
  49. //这儿点击后简单启动Settings模块
  50. PendingIntentcontentIntent=PendingIntent.getActivity
  51. (MainActivity.this,0,newIntent("android.settings.SETTINGS"),0);
  52. noti.contentIntent=contentIntent;
  53. NotificationManagermnotiManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
  54. mnotiManager.notify(0,noti);
  55. }
  56. //默认显示的的Notification
  57. privatevoidshowDefaultNotification(){
  58. //定义Notication的各种属性
  59. CharSequencetitle="iamnew";
  60. inticon=R.drawable.icon;
  61. longwhen=System.currentTimeMillis();
  62. Notificationnoti=newNotification(icon,title,when+10000);
  63. noti.flags=Notification.FLAG_INSISTENT;
  64. //创建一个通知
  65. NotificationmNotification=newNotification();
  66. //设置属性值
  67. mNotification.icon=R.drawable.icon;
  68. mNotification.tickerText="NotificationTest";
  69. mNotification.when=System.currentTimeMillis();//立即发生此通知
  70. //带参数的构造函数,属性值如上
  71. //NotificationmNotification==newNotification(R.drawable.icon,"NotificationTest",System.currentTimeMillis()));
  72. //添加声音效果
  73. mNotification.defaults|=Notification.DEFAULT_SOUND;
  74. //添加震动,后来得知需要添加震动权限:VirbatePermission
  75. //mNotification.defaults|=Notification.DEFAULT_VIBRATE;
  76. //添加状态标志
  77. //FLAG_AUTO_CANCEL该通知能被状态栏的清除按钮给清除掉
  78. //FLAG_NO_CLEAR该通知能被状态栏的清除按钮给清除掉
  79. //FLAG_ONGOING_EVENT通知放置在正在运行
  80. //FLAG_INSISTENT通知的音乐效果一直播放
  81. mNotification.flags=Notification.FLAG_INSISTENT;
  82. //将该通知显示为默认View
  83. PendingIntentcontentIntent=PendingIntent.getActivity
  84. (MainActivity.this,0,newIntent("android.settings.SETTINGS"),0);
  85. mNotification.setLatestEventInfo(MainActivity.this,"通知类型:默认View","一般般哟。。。。",contentIntent);
  86. //设置setLatestEventInfo方法,如果不设置会App报错异常
  87. NotificationManagermNotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
  88. //注册此通知
  89. //如果该NOTIFICATION_ID的通知已存在,会显示最新通知的相关信息,比如tickerText等
  90. mNotificationManager.notify(2,mNotification);
  91. }
  92. privatevoidremoveNotification()
  93. {
  94. NotificationManagermNotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
  95. //取消的只是当前Context的Notification
  96. mNotificationManager.cancel(2);
  97. }
  98. }