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;
/** * 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;
/** * Constructs a Notification object with everything set to 0. * You might want to consider using {@link Builder} instead. */ public Notification() { this.when = System.currentTimeMillis(); }
/** * 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; }
/** * 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; }