Reference/android/app/NotificationManager

From Android中文网

Android中文网(androidcn.net) 版权申明 : creativecommons licenses
Jump to: navigation, search

android.app public class android.app.NotificationManager java.lang.Object

目录

[编辑] android.app.NotificationManager

Class to notify the user of events that happen. This is how you tell the user that something has happened in the background. 这是一个用来通知手机使用者有事件发生的类。用来告诉使用者在后台有一些事情发生了。

Notifications can take different forms: 这些通知可以采用以下一些不同的方式:

   * A View object displayed temporarily when the event occurs,
   * 当事件发生时临时显示一个 View 对象
   * A persistent icon that goes in the status bar and is accessible through the launcher, (when the user selects it, a designated Intent can be launched),
   * 在状态栏上显示一个图标,并且通过图标能够激活,(当用户激活图标时能够激活特定 Intent)
   * Turning on or flashing LEDs on the device, or
   * 打开或者闪烁设备上的LED灯,或
   * Alerting the user by flashing the backlight, playing a sound, or vibrating.
   * 通过闪烁背光,播放声音,或者振动提示用户

To alert the user with a notification that pops up a quick message to the user and then fades it away, use either the notifyWithView(int, View, int, Notification) or the notifyWithText(int, CharSequence, int, Notification) methods on this class. 可以使用类中提供的notifyWithView(int, View, int, Notification)the notifyWithText(int, CharSequence, int, Notification) 方法弹出一个消息框提示用户,然后该消息框渐隐消失。

The notifyWithText method simply constructs a TextView with the supplied string. These methods also take a Notification object, which lets you specify a persistent alert method and other alert properties. To alert the user solely with an intent or status bar icon, use the notify() method. notifyWithText 方法只是构造一个带提示文字的 TextView 对象。这些方法也产生一个 Notification 对象,该对象是你能够设定一个固定的提示方式和其它一些属性。如果要使用一个 Intent 或状态栏单独的提示使用者,可以使用 notify() 方法。

When the view is shown to the user, appears as a floating view over the application. It will never receive focus. The user will probably be in the middle of typing something else. The idea is to be as unobtrusive as possible, while still showing the user the information you want them to see. Two examples are the volume control, and the brief message saying a new SMS has been recieved. 当提示信息框(View 对象)被显示给使用者时,是作为一个浮动在当前应用(application)之上的 View 对象出现的。它永远不能接受焦点(focus),因为用户可能正在录入信息或在做其它什么事情。尽管如此,当仍然显示你想给用户所看的信息时,一个好主意是尽量避免唐突。音量控制和短消息到达提示是两个可以参考的例子。

Each of the notify methods takes an int id parameter. This id identifies this notification from your app to the system, so that id should be unique within your app. If you call one of the notify methods with an id that is currently active and a new set of notification parameters, it will be updated. For example, if you pass a new status bar icon, the old icon in the status bar will be replaced with the new one. This is also the same id you pass to the cancel(int) method to clear this notification. 每一个通知方法都带一个整型的 id 参数。从应用到系统这个 id 参数唯一标识了这次通知,因此,在你的应用里要确保 id 唯一。如果你调用一个通知方法,使用当前在用通知的 id 并且传递一组新的通知参数,则该通知将被更新。 例如,你传递一个新的状态栏图标,位于状态栏的原先的图标将被新的代替。同样,传递同一个id给 cancel(int) 方法将清除该通知。

You do not instantiate this class directly; instead, retrieve it through getSystemService(String). 该类不需要实例化,而是通过调用 getSystemService(String) 得到。

请参阅(See Also)

   * Notification
   * getSystemService(String)

[编辑] Summary

[编辑] Constants

 	  	  	Value 	 

int LENGTH_LONG Show the view or text notification for a long period of time. 1 0x00000001 int LENGTH_SHORT Show the view or text notification for a short period of time. 0 0x00000000 Public Methods

 	  	  	  	void  	cancel(int id)

Cancel a previously shown notification.

 	  	  	static  	NotificationManager  	from(Context context)

Looks up the notification manager within the given context.

 	  	  	  	void  	notify(int id, Notification notification)

Persistent notification on the status bar,

 	  	  	  	void  	notifyWithText(int id, CharSequence text, int viewDuration, Notification notification)

Put up a notification, including a message that is shown briefly to the user.

 	  	  	  	void  	notifyWithText(int id, CharSequence text, int viewDuration, Notification notification, int gravity, float horizontalMargin, float verticalMargin)

Put up a notification, including a message that is shown briefly to the user at the specified location on the screen.

 	  	  	  	void  	notifyWithText(int id, CharSequence text, int viewDuration, Notification notification, int gravity)

Put up a notification, including a message that is shown briefly to the user at the specified location on the screen.

 	  	  	  	void  	notifyWithView(int id, View view, int viewDuration, Notification notification)

Put up a notification, including a view that is shown briefly to the user.

 	  	  	  	void  	notifyWithView(int id, View view, int viewDuration, Notification notification, int gravity, float horizontalMargin, float verticalMargin)

Put up a notification, including a view that is shown briefly to the user at the specified location on screen.

 	  	  	  	void  	notifyWithView(int id, View view, int viewDuration, Notification notification, int gravity)

Put up a notification, including a view that is shown briefly to the user at the specified location on screen. Methods inherited from class java.lang.Object clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

 	  	  	  	Object  	clone()

Answers a new instance of the same class as the receiver, whose slots have been filled in with the values in the slots of the receiver.

 	  	  	  	boolean  	equals(Object o)

Compares the argument to the receiver, and answers true if they represent the same object using a class specific comparison.

 	  	  	  	void  	finalize()

Called by the virtual machine when there are no longer any (non-weak) references to the receiver.

 	  	final  	  	Class  	getClass()

Answers the unique instance of java.lang.Class which represents the class of the receiver.

 	  	  	  	int  	hashCode()

Answers an integer hash code for the receiver.

 	  	final  	  	void  	notify()

Causes one thread which is wait ing on the receiver to be made ready to run.

 	  	final  	  	void  	notifyAll()

Causes all threads which are wait ing on the receiver to be made ready to run.

 	  	  	  	String  	toString()

Answers a string containing a concise, human-readable description of the receiver.

 	  	final  	  	void  	wait(long time, int frac)

Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated by notify or notifyAll) or the expiration of the timeout.

 	  	final  	  	void  	wait(long time)

Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated by notify or notifyAll) or the expiration of the timeout.

 	  	final  	  	void  	wait()

Causes the thread which sent this message to be made not ready to run pending some change in the receiver (as indicated by notify or notifyAll).

[编辑] Details

[编辑] Constants

public static final int LENGTH_LONG Show the view or text notification for a long period of time. This time could be user-definable. Constant Value: 1 (0x00000001) public static final int LENGTH_SHORT Show the view or text notification for a short period of time. This time could be user-definable. Constant Value: 0 (0x00000000)

[编辑] Public Methods

public void cancel(int id) Cancel a previously shown notification. If it's transient, the view will be hidden. If it's persistent, it will be removed from the status bar. public static NotificationManager from(Context context) Looks up the notification manager within the given context. public void notify(int id, Notification notification) Persistent notification on the status bar, Parameters id An identifier for this notification unique within your application. notification A Notification object describing how to notify the user, other than the view you're providing. If you pass null, there will be no persistent notification and no flashing, vibration, etc. public void notifyWithText(int id, CharSequence text, int viewDuration, Notification notification) Put up a notification, including a message that is shown briefly to the user. Parameters id A identifier for this notification unique within your application. text The message to show. It is a CharSequence passed into a TextView, so it can be formatted text. viewDuration Pass either LENGTH_SHORT or LENGTH_LONG for this parameter. notification A Notification object describing how to notify the user, other than the view you're providing. If you pass null, there will be no persistent notification and no flashing, vibration, etc. public void notifyWithText(int id, CharSequence text, int viewDuration, Notification notification, int gravity, float horizontalMargin, float verticalMargin) Put up a notification, including a message that is shown briefly to the user at the specified location on the screen. Parameters id A identifier for this notification unique within your application. text The message to show. It is a CharSequence passed into a TextView, so it can be formatted text. viewDuration Pass either LENGTH_SHORT or LENGTH_LONG for this parameter. notification A Notification object describing how to notify the user, other than the view you're providing. If you pass null, there will be no persistent notification and no flashing, vibration, etc. gravity The location at which the notification should appear on the screen horizontalMargin The horizontal margin, in percentage of the container width, between the container's edges and the notification verticalMargin The vertical margin, in percentage of the container height, between the container's edges and the notification public void notifyWithText(int id, CharSequence text, int viewDuration, Notification notification, int gravity) Put up a notification, including a message that is shown briefly to the user at the specified location on the screen. Parameters id A identifier for this notification unique within your application. text The message to show. It is a CharSequence passed into a TextView, so it can be formatted text. viewDuration Pass either LENGTH_SHORT or LENGTH_LONG for this parameter. notification A Notification object describing how to notify the user, other than the view you're providing. If you pass null, there will be no persistent notification and no flashing, vibration, etc. gravity The location at which the notification should appear on the screen public void notifyWithView(int id, View view, int viewDuration, Notification notification) Put up a notification, including a view that is shown briefly to the user. Parameters id A identifier for this notification unique within your application. view The view to show. viewDuration Pass either LENGTH_SHORT or LENGTH_LONG for this parameter. notification A Notification object describing how to notify the user, other than the view you're providing. If you pass null, there will be no persistent notification and no flashing, vibration, etc. public void notifyWithView(int id, View view, int viewDuration, Notification notification, int gravity, float horizontalMargin, float verticalMargin) Put up a notification, including a view that is shown briefly to the user at the specified location on screen. Parameters id A identifier for this notification unique within your application. view The view to show. viewDuration Pass either LENGTH_SHORT or LENGTH_LONG for this parameter. notification A Notification object describing how to notify the user, other than the view you're providing. If you pass null, there will be no persistent notification and no flashing, vibration, etc. gravity The location at which the notification should appear on the screen horizontalMargin The horizontal margin, in percentage of the container width, between the container's edges and the notification verticalMargin The vertical margin, in percentage of the container height, between the container's edges and the notification public void notifyWithView(int id, View view, int viewDuration, Notification notification, int gravity) Put up a notification, including a view that is shown briefly to the user at the specified location on screen. Parameters id A identifier for this notification unique within your application. view The view to show. viewDuration Pass either LENGTH_SHORT or LENGTH_LONG for this parameter. notification A Notification object describing how to notify the user, other than the view you're providing. If you pass null, there will be no persistent notification and no flashing, vibration, etc. gravity The location at which the notification should appear on the screen

Personal tools