NotificationCompat.Builder
public static class NotificationCompat.Builder
extends Object
Known Direct Subclasses
|
NotificationCompat
对象的生成器类。 允许更轻松地控制所有标志,并帮助构建典型的通知布局。
在不提供扩展通知的平台版本上,依赖扩展通知的方法不起作用。
例如,操作按钮不会出现在Android 4.1之前的平台上。 操作按钮取决于扩展通知,这些通知仅适用于Android 4.1及更高版本。
出于这个原因,您应该始终确保应用程序中的Activity
中的通知中的UI控件也可用,并且您应该始终在用户单击通知时启动该Activity
。 为此,请使用setContentIntent()
方法。
摘要(Summary)
公共方法(Public methods) |
NotificationCompat.Builder |
addAction(NotificationCompat.Action action) 向此通知添加一个操作。 |
NotificationCompat.Builder |
addAction(int icon, CharSequence title, PendingIntent intent) 向此通知添加一个操作。 |
NotificationCompat.Builder |
addExtras(Bundle extras) 将其他元数据合并到此通知中。 |
NotificationCompat.Builder |
addPerson(String uri) 添加与此通知相关的人员。 |
Notification |
build() 组合所有已设置的选项并返回一个新的 Notification 对象。 |
NotificationCompat.Builder |
extend(NotificationCompat.Extender extender) 将扩展器应用于此通知构建器。 |
Bundle |
getExtras() 获取此通知构建器使用的当前元数据包。 |
Notification |
getNotification() 此方法已弃用。 改为使用build() 。 |
NotificationCompat.Builder |
setAutoCancel(boolean autoCancel) 设置此标志将使其通知被自动取消,当用户在面板中单击它时。 |
NotificationCompat.Builder |
setCategory(String category) 设置通知类别。 |
NotificationCompat.Builder |
setColor(int argb) 套 color 。 |
NotificationCompat.Builder |
setContent(RemoteViews views) 提供一个自定义的RemoteViews来代替标准的RemoteViews。 |
NotificationCompat.Builder |
setContentInfo(CharSequence info) 在通知的右侧设置大文本。 |
NotificationCompat.Builder |
setContentIntent(PendingIntent intent) 当点击通知时提供一个 PendingIntent 发送。 |
NotificationCompat.Builder |
setContentText(CharSequence text) 在标准通知中设置通知的文本(第二行)。 |
NotificationCompat.Builder |
setContentTitle(CharSequence title) 在标准通知中设置通知的标题(第一行)。 |
NotificationCompat.Builder |
setCustomBigContentView(RemoteViews contentView) 提供自定义RemoteViews,以代替展开形式的平台模板。 |
NotificationCompat.Builder |
setCustomContentView(RemoteViews contentView) 提供使用自定义RemoteViews来代替平台模板。 |
NotificationCompat.Builder |
setCustomHeadsUpContentView(RemoteViews contentView) 提供自定义RemoteViews,以便在抬头对话框中使用而不是平台模板。 |
NotificationCompat.Builder |
setDefaults(int defaults) 设置将使用的默认通知选项。 |
NotificationCompat.Builder |
setDeleteIntent(PendingIntent intent) 当通知被用户直接从通知面板清除时,提供一个 PendingIntent 发送。 |
NotificationCompat.Builder |
setExtras(Bundle extras) 为此通知设置元数据。 |
NotificationCompat.Builder |
setFullScreenIntent(PendingIntent intent, boolean highPriority) 意图启动而不是将通知发布到状态栏。 |
NotificationCompat.Builder |
setGroup(String groupKey) 将此通知设置为共享相同密钥的一组通知的一部分。 |
NotificationCompat.Builder |
setGroupSummary(boolean isGroupSummary) 将此通知设置为一组通知的组摘要。 |
NotificationCompat.Builder |
setLargeIcon(Bitmap icon) 设置股票和通知中显示的大图标。 |
NotificationCompat.Builder |
setLights(int argb, int onMs, int offMs) 设置您希望设备上的LED指示灯的argb值以及速率。 |
NotificationCompat.Builder |
setLocalOnly(boolean b) 设置此通知是否仅与当前设备相关。 |
NotificationCompat.Builder |
setNumber(int number) 在通知的右侧设置大号。 |
NotificationCompat.Builder |
setOngoing(boolean ongoing) 设置这是否为正在进行的通知。 |
NotificationCompat.Builder |
setOnlyAlertOnce(boolean onlyAlertOnce) 设置此标志,如果您只希望在通知尚未显示的情况下播放声音,振动和播报。 |
NotificationCompat.Builder |
setPriority(int pri) 设置此通知的相对优先级。 |
NotificationCompat.Builder |
setProgress(int max, int progress, boolean indeterminate) 设置此通知所代表的进度,可能表示为 ProgressBar 。 |
NotificationCompat.Builder |
setPublicVersion(Notification n) 提供替换通知,其内容应显示在不安全的上下文中(即, |
NotificationCompat.Builder |
setRemoteInputHistory(CharSequence[] text) 设置远程输入历史记录。 |
NotificationCompat.Builder |
setShowWhen(boolean show) 控制是否在内容视图中显示使用 setWhen 设置的时间戳。 |
NotificationCompat.Builder |
setSmallIcon(int icon, int level) setSmallIcon(int) 一种变体,当图标是 LevelListDrawable 时,需要额外的级别参数。 |
NotificationCompat.Builder |
setSmallIcon(int icon) 设置通知布局中使用的小图标。 |
NotificationCompat.Builder |
setSortKey(String sortKey) 设置一个排序键,在同一个包中的其他通知中排列此通知。 |
NotificationCompat.Builder |
setSound(Uri sound) 设置声音播放。 |
NotificationCompat.Builder |
setSound(Uri sound, int streamType) 设置声音播放。 |
NotificationCompat.Builder |
setStyle(NotificationCompat.Style style) 添加丰富的通知样式,以便在构建时应用。 |
NotificationCompat.Builder |
setSubText(CharSequence text) 在平台通知模板中设置第三行文字。 |
NotificationCompat.Builder |
setTicker(CharSequence tickerText, RemoteViews views) 设置通知第一次到达时状态栏中显示的文本,以及可能在某些设备上显示的RemoteViews对象。 |
NotificationCompat.Builder |
setTicker(CharSequence tickerText) 设置通知第一次到达时状态栏中显示的文本。 |
NotificationCompat.Builder |
setUsesChronometer(boolean b) 将 when 字段显示为秒表。 |
NotificationCompat.Builder |
setVibrate(long[] pattern) 设置要使用的振动模式。 |
NotificationCompat.Builder |
setVisibility(int visibility) 套 visibility 。 |
NotificationCompat.Builder |
setWhen(long when) 设置事件发生的时间。 |
Fields
Public constructors
NotificationCompat.Builder
NotificationCompat.Builder (Context context)
构造函数。 自动将when字段设置为System.currentTimeMillis()
,将音频流自动设置为STREAM_DEFAULT
。
参数(Parameters) |
context |
Context : A Context that will be used to construct the RemoteViews. The Context will not be held past the lifetime of this Builder object. |
公共方法(Public methods)
getExtras
Bundle getExtras ()
获取此通知构建器使用的当前元数据包。
返回的Bundle与此Builder共享。
每次调用 build()
,该Bundle的当前内容都会复制到通知中。
setFullScreenIntent
NotificationCompat.Builder setFullScreenIntent (PendingIntent intent,
boolean highPriority)
意图启动而不是将通知发布到状态栏。 仅用于需要用户立即关注的极高优先级通知,例如用户明确设置为特定时间的来电或闹钟。 如果此设施用于其他用途,请给用户一个关闭它并使用正常通知的选项,因为这可能会造成极大的干扰。
在某些平台上,当用户使用设备时,系统UI可能会选择显示抬头通知,而不是启动此意图。
参数(Parameters) |
intent |
PendingIntent : The pending intent to launch. |
highPriority |
boolean : Passing true will cause this notification to be sent even if other notifications are suppressed. |
setGroupSummary
NotificationCompat.Builder setGroupSummary (boolean isGroupSummary)
将此通知设置为一组通知的组摘要。 分组通知可以显示在支持这种呈现的设备上的群集或堆栈中。 还需要使用setGroup(String)
来设置组密钥。
参数(Parameters) |
isGroupSummary |
boolean : Whether this notification should be a group summary. |
setLights
NotificationCompat.Builder setLights (int argb,
int onMs,
int offMs)
设置您希望设备上的LED指示灯的argb值以及速率。 速率根据要开启的毫秒数指定,然后指定关闭的毫秒数。
参数(Parameters) |
argb |
int
|
onMs |
int
|
offMs |
int
|
setNumber
NotificationCompat.Builder setNumber (int number)
在通知的右侧设置大号。 这相当于setContentInfo,尽管它可能为了可读性而显示不同字体大小的数字。
参数(Parameters) |
number |
int
|
setOngoing
NotificationCompat.Builder setOngoing (boolean ongoing)
设置这是否为正在进行的通知。
正在进行的通知与正常通知的区别在于以下方面:
- Ongoing notifications are sorted above the regular notifications in the notification panel.
- Ongoing notifications do not have an 'X' close button, and are not affected by the "Clear all" button.
参数(Parameters) |
ongoing |
boolean
|
setOnlyAlertOnce
NotificationCompat.Builder setOnlyAlertOnce (boolean onlyAlertOnce)
设置此标志,如果您只希望在通知尚未显示的情况下播放声音,振动和播报。
参数(Parameters) |
onlyAlertOnce |
boolean
|
setPriority
NotificationCompat.Builder setPriority (int pri)
设置此通知的相对优先级。 优先级指示该通知应该消耗多少用户的有价值的关注。 在某些情况下,用户可能会隐藏低优先级通知,而用户可能因高优先级通知而被中断。 系统根据各种因素(包括setPriority值)设置通知的优先级。 效果可能会在不同的平台上略有不同。
setProgress
NotificationCompat.Builder setProgress (int max,
int progress,
boolean indeterminate)
设置此通知代表的进度,可能表示为 ProgressBar
。
参数(Parameters) |
max |
int
|
progress |
int
|
indeterminate |
boolean
|
setRemoteInputHistory
NotificationCompat.Builder setRemoteInputHistory (CharSequence[] text)
设置远程输入历史记录。 这应该设置为通过本通知的RemoteInput
发送的最新输入,并在其不再相关时清除(例如,一旦对方已响应,则发送聊天通知)。 最近的输入必须存储在0索引处,第一个索引处的第二个最近的输入等。请注意,系统将限制显示输入的多远以及显示每个输入的多少。
注意:回复文本只会显示在至少有一个动作为 RemoteInput
通知中。
参数(Parameters) |
text |
CharSequence
|
setSmallIcon
NotificationCompat.Builder setSmallIcon (int icon)
设置通知布局中使用的小图标。 不同类别的设备可能会返回不同的尺寸。 有关如何设计这些图标的更多信息,请参阅用户体验指南。
参数(Parameters) |
icon |
int : A resource ID in the application's package of the drawble to use. |
setSortKey
NotificationCompat.Builder setSortKey (String sortKey)
设置一个排序键,在同一个包中的其他通知中排列此通知。 如果已经应用了外部排序并且应用程序想要保留这一点,这可能很有用。 通知将按照字典顺序使用此值进行排序,但除了提供排序键之外,提供不同的优先级可能会导致忽略此值。
此排序关键字也可用于订购通知组的成员。 见setGroup(String)
。
参数(Parameters) |
sortKey |
String
|
setSound
NotificationCompat.Builder setSound (Uri sound,
int streamType)
设置声音播放。 它将在您提供的流上播放。
在某些平台上,嘈杂的通知更有可能作为单挑通知呈现。
参数(Parameters) |
sound |
Uri
|
streamType |
int
|
setUsesChronometer
NotificationCompat.Builder setUsesChronometer (boolean b)
将when
字段显示为秒表。 不是将when
作为时间戳显示,而是显示自when
以来的分钟和秒的自动更新显示。 显示经过时间(如正在进行的电话)时很有用。
setVibrate
NotificationCompat.Builder setVibrate (long[] pattern)
设置要使用的振动模式。
在某些平台上,发生振动的通知更有可能以提醒通知的形式呈现。
参数(Parameters) |
pattern |
long
|
Protected methods