android源码浅析-notification
发布时间: 2013-01-27 13:56:17 作者: rapoo
android源码浅析--notification
在上一篇文章中,用到了Notification,准备好好的看下notification的源码。
类概述:
一个表示如何使用NotificationManager把一个持久的通知呈现给用户的类。
The Notification.Builder
has been added to make it easier to construct Notifications.
添加Notification.Builder使构建Notification更容易。
静态变量:
/** * Bit to be bitwise-ored into the {@link #flags} field that should be * set if you want the LED on for this notification. * <ul> * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB * or 0 for both ledOnMS and ledOffMS.</li> * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li> * <li>To flash the LED, pass the number of milliseconds that it should * be on and off to ledOnMS and ledOffMS.</li> * </ul> * <p> * Since hardware varies, you are not guaranteed that any of the values * you pass are honored exactly. Use the system defaults (TODO) if possible * because they will be set to values that work on any given hardware. * <p> * The alpha channel must be set for forward compatibility. * */ public static final int FLAG_SHOW_LIGHTS = 0x00000001; /** * Bit to be bitwise-ored into the {@link #flags} field that should be * set if this notification is in reference to something that is ongoing, * like a phone call. It should not be set if this notification is in * reference to something that happened at a particular point in time, * like a missed phone call. */ public static final int FLAG_ONGOING_EVENT = 0x00000002; /** * Bit to be bitwise-ored into the {@link #flags} field that if set, * the audio will be repeated until the notification is * cancelled or the notification window is opened. */ public static final int FLAG_INSISTENT = 0x00000004; /** * Bit to be bitwise-ored into the {@link #flags} field that should be * set if you want the sound and/or vibration play each time the * notification is sent, even if it has not been canceled before that. */ public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008; /** * Bit to be bitwise-ored into the {@link #flags} field that should be * set if the notification should be canceled when it is clicked by the * user. On tablets, the */ public static final int FLAG_AUTO_CANCEL = 0x00000010; /** * Bit to be bitwise-ored into the {@link #flags} field that should be * set if the notification should not be canceled when the user clicks * the Clear all button. */ public static final int FLAG_NO_CLEAR = 0x00000020; /** * Bit to be bitwise-ored into the {@link #flags} field that should be * set if this notification represents a currently running service. This * will normally be set for you by {@link Service#startForeground}. */ public static final int FLAG_FOREGROUND_SERVICE = 0x00000040; /** * Bit to be bitwise-ored into the {@link #flags} field that should be set if this notification * represents a high-priority event that may be shown to the user even if notifications are * otherwise unavailable (that is, when the status bar is hidden). This flag is ideally used * in conjunction with {@link #fullScreenIntent}. */ public static final int FLAG_HIGH_PRIORITY = 0x00000080;
FLAG_SHOW_LIGHTS:如果想为Notification添加LED灯提示,需要在Flag属性中添加FLAG_SHOW_LIGHT标志位。LED关闭状态下,0表示颜色或者LED灯开关LED开启状态下,1代表LED开,0代表LED关。LED闪烁状态下,ledOnMS表示灯亮时间,ledOffMS表示灯灭时间。因为硬件不一,所以不能保证在各种硬件上都能够奏效,应尽可能使用系统给定的默认值(TODO),它能够适配各种硬件。alpha通道必须设置为向前兼容性
FLAG_ONGOING_EVENT:如果需要notification常驻状态栏,引用正在进行的活动,如一个打进的电话,需要在Flag属性中添加FLAG_ONGOING_EVENT标志位。但是其不能引用一个过去已经发生的活动,如一个未接电话。(设置其Flag,notification将被放入“正在进行”组)
FLAG_INSISTENT:设置此Flag,在通知取消或通知窗口打开之前,音频会一直重复。
FLAG_ONLY_ALERT_ONCE:设置此Flag,震动和声音只执行一次。FLAG_AUTO_CANCEL:
设置此Flag,当用户点击时,通知会被清除。FLAG_NO_CLEAR:设置此Flag,当用户点击清除所有按钮时,该通知不被清除。FLAG_FOREGROUND_SERVICE:设置此Flag,表示该通知为正运行的服务(当你退出手机QQ时,在状态栏会看到一个QQ图标)FLAG_HIGH_PRIORITY:一个过时的Flag,现在已使用优先级字段代替。设置该Flag表示一高优先级事件,当通知不可以使用(状态栏被隐藏时)也可通知用户,应配合fullScreenIntent使用。
常量 /** * The timestamp for the notification. The icons and expanded views * are sorted by this key. */ public long when; /** * The resource id of a drawable to use as the icon in the status bar. * This is required; notifications with an invalid icon resource will not be shown. */ public int icon; /** * If the icon in the status bar is to have more than one level, you can set this. Otherwise, * leave it at its default value of 0. * * @see android.widget.ImageView#setImageLevel * @see android.graphics.drawable#setLevel */ public int iconLevel; /** * The number of events that this notification represents. For example, in a new mail * notification, this could be the number of unread messages. This number is superimposed over * the icon in the status bar. If the number is 0 or negative, it is not shown in the status * bar. */ public int number; /** * The intent to execute when the expanded status entry is clicked. If * this is an activity, it must include the * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires * that you take care of task management as described in the * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back * Stack</a> document. In particular, make sure to read the notification section * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling * Notifications</a> for the correct ways to launch an application from a * notification. */ public PendingIntent contentIntent; /** * The intent to execute when the status entry is deleted by the user * with the "Clear All Notifications" button. This probably shouldn't * be launching an activity since several of those will be sent at the * same time. */ public PendingIntent deleteIntent; /** * An intent to launch instead of posting the notification to the status bar. * * @see Notification.Builder#setFullScreenIntent */ public PendingIntent fullScreenIntent; /** * Text to scroll across the screen when this item is added to * the status bar on large and smaller devices. * * <p>This field is provided separately from the other ticker fields * both for compatibility and to allow an application to choose different * text for when the text scrolls in and when it is displayed all at once * in conjunction with one or more icons. * * @see #tickerView */ public CharSequence tickerText; /** * The view to show as the ticker in the status bar when the notification * is posted. */ public RemoteViews tickerView; /** * The view that will represent this notification in the expanded status bar. */ public RemoteViews contentView; /** * The bitmap that may escape the bounds of the panel and bar. */ public Bitmap largeIcon; /** * The sound to play. * * <p> * To play the default notification sound, see {@link #defaults}. * </p> */ public Uri sound;
when:通知的时间戳,通知图标会根据该时间戳在状态栏进行排序。icon:
在状态栏中显示的图标,如果是无效资源,不予显示。iconLevel:
如果状态栏中的icon有等级之分,可以设置此参数,默认情况下,一般为0.number:
通知事件发生的次数,比如一个邮件通知,这个参数表示未读邮件数。这个数字会叠加到状态栏中通知图标上,如果为0或者负,则不显示。contentIntent:
The intent to execute when the expanded status entry is clicked.当扩展状态内容被点击时触发该intent(expanded status entry不知道该如何翻译是好了
)如果是要跳转到一个Activity,一定要包含FLAG_ACTIVITY_NEW_TASK这个标志位,用于调整堆栈中的Task。能够确保正确的从一个通知进入一个应用。deleteIntent:
当用户点击“clean all button"或者滑动取消通知时触发该Intent,不能启动一个activity或者server(因为会在同一时间发送)。fullScreenIntent:全屏状态下代替状态栏中intent功能tickerText:
当通知发送时像股票一样提示文本。contentView:在状态栏中代表Notification的View。largeIcon:
可以脱离状态栏的图片sound:通知声音audioStreamType:
视频播放流类型vibrate:
震动类型ledARGB:
LED灯颜色ledOnMS:
LED灯闪烁时,灯亮时间。ledOffMS:
LED灯闪烁时,灯灭时间。defaults:
LED默认值。
构造函数默认构造函数: /** * Constructs a Notification object with everything set to 0. * You might want to consider using {@link Builder} instead. */ public Notification() { this.when = System.currentTimeMillis(); }
可以用Notification.Builder来替代构造一个Notification(关于Builder建造者模式 http://blog.csdn.net/aomandeshangxiao/article/details/8214646)。
/** * An intent to launch instead of posting the notification to the status bar. * Only for use with extremely high-priority notifications demanding the user's * <strong>immediate</strong> attention, such as an incoming phone call or * alarm clock that the user has explicitly set to a particular time. * If this facility is used for something else, please give the user an option * to turn it off and use a normal notification, as this can be extremely * disruptive. * * @param intent The pending intent to launch. * @param highPriority Passing true will cause this notification to be sent * even if other notifications are suppressed. */ public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) { mFullScreenIntent = intent; setFlag(FLAG_HIGH_PRIORITY, highPriority); return this; }
一个全屏状态下替代状态栏上的Notification的intent,仅适用于使用非常高的优先级通知,会引起用户的即时关注,如用户已显式设置为一个特定的时间,一个来电或闹钟。
设置TickerText: /** * Combine all of the options that have been set and return a new {@link Notification} * object. */ public Notification getNotification() { Notification n = new Notification(); n.when = mWhen; n.icon = mSmallIcon; n.iconLevel = mSmallIconLevel; n.number = mNumber; n.contentView = makeContentView(); n.contentIntent = mContentIntent; n.deleteIntent = mDeleteIntent; n.fullScreenIntent = mFullScreenIntent; n.tickerText = mTickerText; n.tickerView = makeTickerView(); n.largeIcon = mLargeIcon; n.sound = mSound; n.audioStreamType = mAudioStreamType; n.vibrate = mVibrate; n.ledARGB = mLedArgb; n.ledOnMS = mLedOnMs; n.ledOffMS = mLedOffMs; n.defaults = mDefaults; n.flags = mFlags; if (mLedOnMs != 0 && mLedOffMs != 0) { n.flags |= FLAG_SHOW_LIGHTS; } if ((mDefaults & DEFAULT_LIGHTS) != 0) { n.flags |= FLAG_SHOW_LIGHTS; } return n; }
Builder的最后,提供一个完整的产品出来。
PS:写到这,正文部分大体完成,现在时间也已经是晚上11点钟,1000多行的代码,推迟了3天才把文章写完,进度确实有点慢,最近在读凯利麦格尼格尔女士的《自控力》这本书,看到“道德许可效应”,解决了自己关于很多事情的疑问。还没有读完,感觉是一本不错的书籍,推荐一下。另外,周五有次重要的谈话,祝自己顺利。