android-Notification通知

Android的Toast 类提供了一个方便的方式来显示用户的警告信息,但这些警告不是持久性的,这意味着警告闪烁在屏幕上几秒钟后就消失了。

对于特别重要的要提供给用户的消息,需要有更持久性的方法。Notification是一种消息可以显示在设备的顶部的通知栏或状态栏中。

分类

 notification有以下几种:

  1>普通notification

  android-Notification通知_第1张图片

    1.内容标题

    2.大图标

    3.内容

    4.内容附加信息

    5.小图标

    6.时间

  2>大布局Notification

    android-Notification通知_第2张图片图1

  大布局notification是在android4.1以后才增加的,大布局notification与小布局notification只在‘7'-通知细节区域部分有区别,其它部分都一致。

通知细节区域。对于此区域的显示内容,各种大型视图通
知有着不同的设置:
Big picture style:细节区域展现一个高度为256dp的bitmap。
Big text style:细节区域显示一个大型文本块。
Inbox style:细节区域包含文本行数。


大布局notification只有在所有notification的最上面时才会显示大布局,其它情况下显示小布局。你也可以用手指将其扩展为大布局(前提是它是大布局)。如下图:

  android-Notification通知_第3张图片图2

    大布局notification有三种类型:如图1为NotificationCompat.InboxStyle 类型。图2左部为NotificationCompat.BigTextStyle。图2右部 为:NotificationCompat.BigPictureStyle


   3>自定义布局notification

     除了系统提供的notification,我们也可以自定义notification。如下图所示的一个音乐播放器控制notification:

    android-Notification通知_第4张图片图3

如何创建notification


    1>实例化一个NotificationCompat.Builder对象;如builder

     2>调用builder的相关方法对notification进行上面提到的各种设置

    3>调用builder.build()方法此方法返回一个notification对象。

     4>实例化一个NotificationManager对象;如:manager

     5>调用manager的notify方法。

  注:

   一个notification不必对上面所有的选项都进行设置,但有3项是必须的:

   小图标, set by setSmallIcon()

     内容标题, set by setContentTitle()

     内容, set by setContentText()

public static class

NotificationCompat.Builder

extends  Object
java.lang.Object
   ↳ android.support.v4.app.NotificationCompat.Builder

类概述


Builder class for NotificationCompat objects. Allows easier control over all the flags, as well as help constructing the typical notification layouts.

On platform versions that don't offer expanded notifications, methods that depend on expanded notifications have no effect.

For example, action buttons won't appear on platforms prior to Android 4.1. Action buttons depend on expanded notifications, which are only available in Android 4.1 and later.

For this reason, you should always ensure that UI controls in a notification are also available in an Activity in your app, and you should always start that Activitywhen users click the notification. To do this, use the setContentIntent() method.

概述


公有构造函数
NotificationCompat.Builder(Context context)
Constructor.
公有方法
NotificationCompat.Builder addAction(int icon, CharSequence title, PendingIntent intent)
Add an action to this notification.
Notification build()
Combine all of the options that have been set and return a new  Notification object.
Notification getNotification()
该 method 从 API 级别 开始已经废弃。 Use build() instead.
NotificationCompat.Builder setAutoCancel(boolean autoCancel)
Setting this flag will make it so the notification is automatically canceled when the user clicks it in the panel.
NotificationCompat.Builder setContent(RemoteViews views)
Supply a custom RemoteViews to use instead of the standard one.
NotificationCompat.Builder setContentInfo(CharSequence info)
Set the large text at the right-hand side of the notification.
NotificationCompat.Builder setContentIntent(PendingIntent intent)
Supply a  PendingIntent to send when the notification is clicked.
NotificationCompat.Builder setContentText(CharSequence text)
Set the text (second row) of the notification, in a standard notification.
NotificationCompat.Builder setContentTitle(CharSequence title)
Set the title (first row) of the notification, in a standard notification.
NotificationCompat.Builder setDefaults(int defaults)
Set the default notification options that will be used.
NotificationCompat.Builder setDeleteIntent(PendingIntent intent)
Supply a  PendingIntent to send when the notification is cleared by the user directly from the notification panel.
NotificationCompat.Builder setFullScreenIntent(PendingIntent intent, boolean highPriority)
An intent to launch instead of posting the notification to the status bar.
NotificationCompat.Builder setLargeIcon(Bitmap icon)
Set the large icon that is shown in the ticker and notification.
NotificationCompat.Builder setLights(int argb, int onMs, int offMs)
Set the argb value that you would like the LED on the device to blnk, as well as the rate.
NotificationCompat.Builder setNumber(int number)
Set the large number at the right-hand side of the notification.
NotificationCompat.Builder setOngoing(boolean ongoing)
Set whether this is an ongoing notification.
NotificationCompat.Builder setOnlyAlertOnce(boolean onlyAlertOnce)
Set this flag if you would only like the sound, vibrate and ticker to be played if the notification is not already showing.
NotificationCompat.Builder setPriority(int pri)
Set the relative priority for this notification.
NotificationCompat.Builder setProgress(int max, int progress, boolean indeterminate)
Set the progress this notification represents, which may be represented as a  ProgressBar.
NotificationCompat.Builder setSmallIcon(int icon, int level)
A variant of  setSmallIcon(int) that takes an additional level parameter for when the icon is a  LevelListDrawable.
NotificationCompat.Builder setSmallIcon(int icon)
Set the small icon to use in the notification layouts.
NotificationCompat.Builder setSound(Uri sound)
Set the sound to play.
NotificationCompat.Builder setSound(Uri sound, int streamType)
Set the sound to play.
NotificationCompat.Builder setStyle(NotificationCompat.Style style)
Add a rich notification style to be applied at build time.
NotificationCompat.Builder setSubText(CharSequence text)
Set the third line of text in the platform notification template.
NotificationCompat.Builder setTicker(CharSequence tickerText, RemoteViews views)
Set the text that is displayed in the status bar when the notification first arrives, and also a RemoteViews object that may be displayed instead on some devices.
NotificationCompat.Builder setTicker(CharSequence tickerText)
Set the text that is displayed in the status bar when the notification first arrives.
NotificationCompat.Builder setUsesChronometer(boolean b)
Show the  when field as a stopwatch.
NotificationCompat.Builder setVibrate(long[] pattern)
Set the vibration pattern to use.
NotificationCompat.Builder setWhen(long when)
Set the time that the event occurred.
[展开]
继承方法
 From class java.lang.Object

公有构造函数


public NotificationCompat.Builder (Context context)

Constructor. Automatically sets the when field to System.currentTimeMillis() and the audio stream to the STREAM_DEFAULT.

参数
context Context that will be used to construct the RemoteViews. The Context will not be held past the lifetime of this Builder object.

公有方法


public NotificationCompat.Builder addAction (int icon, CharSequence title, PendingIntent intent)

Add an action to this notification. Actions are typically displayed by the system as a button adjacent to the notification content. 
Action buttons won't appear on platforms prior to Android 4.1. Action buttons depend on expanded notifications, which are only available in Android 4.1 and later. To ensure that an action button's functionality is always available, first implement the functionality in the Activity that starts when a user clicks the notification (seesetContentIntent()), and then enhance the notification by implementing the same functionality with addAction().

参数
icon Resource ID of a drawable that represents the action.
title Text describing the action.
intent PendingIntent to be fired when the action is invoked.

public Notification build ()

Combine all of the options that have been set and return a new Notification object.

public Notification getNotification ()

该 method 从 API 级别 开始已经废弃。
Use build() instead.

public NotificationCompat.Builder setAutoCancel (boolean autoCancel)

Setting this flag will make it so the notification is automatically canceled when the user clicks it in the panel. The PendingIntent set withsetDeleteIntent(PendingIntent) will be broadcast when the notification is canceled.

public NotificationCompat.Builder setContent (RemoteViews views)

Supply a custom RemoteViews to use instead of the standard one.

public NotificationCompat.Builder setContentInfo (CharSequence info)

Set the large text at the right-hand side of the notification.

public NotificationCompat.Builder setContentIntent (PendingIntent intent)

Supply a PendingIntent to send when the notification is clicked. If you do not supply an intent, you can now add PendingIntents to individual views to be launched when clicked by calling RemoteViews.setOnClickPendingIntent(int,PendingIntent). Be sure to read Notification.contentIntent for how to correctly use this.

public NotificationCompat.Builder setContentText (CharSequence text)

Set the text (second row) of the notification, in a standard notification.

public NotificationCompat.Builder setContentTitle (CharSequence title)

Set the title (first row) of the notification, in a standard notification.

public NotificationCompat.Builder setDefaults (int defaults)

Set the default notification options that will be used.

The value should be one or more of the following fields combined with bitwise-or: DEFAULT_SOUNDDEFAULT_VIBRATEDEFAULT_LIGHTS.

For all default values, use DEFAULT_ALL.

public NotificationCompat.Builder setDeleteIntent (PendingIntent intent)

Supply a PendingIntent to send when the notification is cleared by the user directly from the notification panel. For example, this intent is sent when the user clicks the "Clear all" button, or the individual "X" buttons on notifications. This intent is not sent when the application calls NotificationManager.cancel(int).

public NotificationCompat.Builder setFullScreenIntent (PendingIntent intent, boolean highPriority)

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 immediateattention, 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.

参数
intent The pending intent to launch.
highPriority Passing true will cause this notification to be sent even if other notifications are suppressed.

public NotificationCompat.Builder setLargeIcon (Bitmap icon)

Set the large icon that is shown in the ticker and notification.

public NotificationCompat.Builder setLights (int argb, int onMs, int offMs)

Set the argb value that you would like the LED on the device to blnk, as well as the rate. The rate is specified in terms of the number of milliseconds to be on and then the number of milliseconds to be off.

public NotificationCompat.Builder setNumber (int number)

Set the large number at the right-hand side of the notification. This is equivalent to setContentInfo, although it might show the number in a different font size for readability.

public NotificationCompat.Builder setOngoing (boolean ongoing)

Set whether this is an ongoing notification.

Ongoing notifications differ from regular notifications in the following ways:

  • 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.

public NotificationCompat.Builder setOnlyAlertOnce (boolean onlyAlertOnce)

Set this flag if you would only like the sound, vibrate and ticker to be played if the notification is not already showing.

public NotificationCompat.Builder setPriority (int pri)

Set the relative priority for this notification. Priority is an indication of how much of the user's valuable attention should be consumed by this notification. Low-priority notifications may be hidden from the user in certain situations, while the user might be interrupted for a higher-priority notification. The system sets a notification's priority based on various factors including the setPriority value. The effect may differ slightly on different platforms.

public NotificationCompat.Builder setProgress (int max, int progress, boolean indeterminate)

Set the progress this notification represents, which may be represented as a ProgressBar.

public NotificationCompat.Builder setSmallIcon (int icon, int level)

A variant of setSmallIcon(int) that takes an additional level parameter for when the icon is a LevelListDrawable.

参数
icon A resource ID in the application's package of the drawble to use.
level The level to use for the icon.
参见
  • LevelListDrawable

public NotificationCompat.Builder setSmallIcon (int icon)

Set the small icon to use in the notification layouts. Different classes of devices may return different sizes. See the UX guidelines for more information on how to design these icons.

参数
icon A resource ID in the application's package of the drawble to use.

public NotificationCompat.Builder setSound (Uri sound)

Set the sound to play. It will play on the default stream.

public NotificationCompat.Builder setSound (Uri sound, int streamType)

Set the sound to play. It will play on the stream you supply.

参见
  • ERROR(/#STREAM_DEFAULT)
  • for the STREAM_ constants.

public NotificationCompat.Builder setStyle (NotificationCompat.Style style)

Add a rich notification style to be applied at build time. 
If the platform does not provide rich notification styles, this method has no effect. The user will always see the normal notification style.

参数
style Object responsible for modifying the notification style.

public NotificationCompat.Builder setSubText (CharSequence text)

Set the third line of text in the platform notification template. Don't use if you're also using setProgress(int, int, boolean); they occupy the same location in the standard template. 
If the platform does not provide large-format notifications, this method has no effect. The third line of text only appears in expanded view. 

public NotificationCompat.Builder setTicker (CharSequence tickerText, RemoteViews views)

Set the text that is displayed in the status bar when the notification first arrives, and also a RemoteViews object that may be displayed instead on some devices.

public NotificationCompat.Builder setTicker (CharSequence tickerText)

Set the text that is displayed in the status bar when the notification first arrives.

public NotificationCompat.Builder setUsesChronometer (boolean b)

Show the when field as a stopwatch. Instead of presenting when as a timestamp, the notification will show an automatically updating display of the minutes and seconds since when. Useful when showing an elapsed time (like an ongoing phone call).

参见
  • Chronometer
  • when

public NotificationCompat.Builder setVibrate (long[] pattern)

Set the vibration pattern to use.

参见
  • for a discussion of the pattern parameter.

public NotificationCompat.Builder setWhen (long when)

Set the time that the event occurred. Notifications in the panel are sorted by this time.

你可能感兴趣的:(android,个人理解)