Use of the notification bar in Android

Source: Internet
Author: User

 Hi,众猿们,今天讲讲安卓中通知的使用: 什么是通知:安卓系统用户发送消息的一种方式,当通知发出后,会出现在系统的通知栏上,当用户点击通知时,会进入到相应的界面(想象下当有新短信到来时的情况),一个默认的通知包含的内容为:          ![通知包含的内容](http://img.blog.csdn.net/20160420212314894) 通知的基本使用方式:其代码如下(一般定义在上下文对象(如Activity)中):
 //Get Notification Manager, notification is a system serviceNotificationmanager manager = (Notificationmanager) getsystemservice (Context.notification_service);//Initialize notification object p1: Notification icon p2: Notification status bar display prompt P3: Notification display timeNotification Notification =NewNotification (R.drawable.ic_launcher,"Reminders", System.currenttimemillis ());//Click on the notification after the intent, this example after the click or in the current interfaceIntent descintent =NewIntent ( This, Mainactivity.class); Pendingintent Intent = pendingintent.getactivity ( This,0, Descintent,0);//Set Notification informationNotification.setlatesteventinfo ( This,"Reminders","Wish you every day happy", intent); Notification.flags|=notification.flag_auto_cancel;//When viewed, automatically disappearsNotification.defaults |= Notification.default_sound;//default sound hint //Send notifications,Manager.notify (0, notification);
 From the above code, we can see that the notification programming is mainly related to two icons: Notification and Notificationmanager, let's go to one by one to introduce the object: Notification: Notify the object, we want to show what kind of notice, This object is used to implement 1. Create a Notification object: Call its construction method, its argument list is: int icon: The icon resource ID number of the notification Charsequence Tickertext: Notify when the notification is issued and the user has not yet pulled the notification bar Information displayed on the notification bar long when: Notification time 2.setLatestEventInfo Method: Set the information carried by the notification, its parameter list is: Context context: The object that creates the notification, can be a acti vity, service, etc. charsequence contenttitle: Title charsequence contenttext after user drop-down notification Pending: Content displayed after user drop-down Intent Contentintent: The action to be completed after the user clicks the notification (such as initiating activity, etc.), the action will be delayed, and not the creator of the notification (also the creator of the Pendingintent object) to complete, So to use the Pendingintent object instead of intent object 3.sound field: Object of the URI type, set the sound 4.vibrate field when the notification is emitted: an array of type int that sets the vibration policy when the notification is emitted, such as when the value is: { 0,100,200,300}, it can be said: 0 milliseconds after the start of vibration, vibration 100 milliseconds after the stop, and then after 200 milliseconds again vibration 300 milliseconds 5. Set the flashing of the indicator lights when the notification is emitted, using the following fields: Ledargb: Hexadecimal int value, indicating the        Color, such as: 0xff00ff00; LEDONMS: The time of the indicator light in a cycle LEDOFFMS: The time of the light off in one cycle we can see that the notification sound, vibration and indicator flashes are done by assigning values to the fields in the notification object, and we can even Set the notification interface by assigning a value to the field in the notification (the interface of the custom notification will be described below), set the click NotificationAfter the action and so on, the specific fields should be assigned value, the apes can be consulted by themselves, the ape ape will not elaborate, and then say a few fields: 6.flags:int type value, set the other properties of the notification, the value can be notification objects such as the following Static constants: Flag_auto_ CANCEL: Automatically clear this notification after clicking this notification on the notification bar flag_insistent: Repeat the sound until the user responds to this notification flag_only_alert_once: After the notification is initiated, the ring and vibrate are only executed once FLAG _ongoing_event: Put this notification in the "ongoing" or "running" Group of the notification bar, they are usually used to represent a background task that the user actively participates in (such as playing music) or is waiting in some way, so occupy the device (such as a file download, sync operation, active network connection Flag_no_clear: Indicates that this notification is not cleared after clicking "Clear Notification" in the notification bar, and is often used with flag_ongoing_event flag_show_lights: The indicator is used when the notification is set and we want to set the notification issue This value must be set for flags when the light is shining, when we need to set multiple values for flags, each value should be separated by | 7.defaults: Those properties that set the notification will take the default values, and our other settings for these properties will be invalidated. The value can be the following static constant in the notification object: Default_sound: Set notification using the default beep default_vibrate: Set notification using the default vibration mode default_l Ights: Set notifications to use the default led effect Default_all: Set notifications all with default effects in addition, there is a property field in the notification object that sets the priority of the notification, and no bird uses it, as you can see Noti Ficationmanager: Notification of the Management object, responsible for sending notifications 1. Get the Notificationmanager object: Call the Getserivce method of the context object, Static constants required to pass into the context object: Notification_service as a parameter 2.notify method: sends a notification with the parameter: Int ID: is sentThe notification specifies an ID Notification Notification: Notification object that needs to be sent in fact, the Android system also provides us with another way to create Notification objects, namely: Using Notificationcompat Builder object, the ape apes who know the observer design pattern must understand that this ape will not say much, the layout of the custom notification: When the notification is sent, it will have a default layout on the notification bar, but sometimes this default layout does not meet our requirements, This is our custom layout for notifications: Learn about Remoteviews objects: This object represents a view object in a different process, and we all know that view is an object in the current thread, and when we need to do it for another process (such as: notifications are running in a system process, not in the current process). Therefore, the layout view is not part of the current process, such as the floating window on the desktop, and its layout view is not part of the current process of the object creation view, the use of the Remoteviews object, said here everyone can understand the layout view of the notification is a remoteviews bar, Because we need to know this object first: 1. Map a layout file to a Remoteviews object: Call the constructor of the Remoteviews object, which is the argument list: String PackageName: The package name of the current process, calling the context object Getpackagename () method to get int LayoutID: The resource ID number of the layout file to be mapped 2.RemoteViews object belongs to other processes, so it only supports Framelayout, LinearLayout, R Elativelayout three layout controls and AnalogClock, Chronometer, Button, ImageButton, ImageView, ProgressBar, TextView, Viewflipper, ListView, GridView, StackView, and adapterviewflipper these display controls, and other controls cannot appear in their layout files 3. Set text for controls in the layout file for Remoteviews objects,    Properties such as background images: You can specify properties for the control directly in the layout file, and when we need to set it in code, call Remoteviews in the following ways: Setimageviewresource: Setting up a picture resource for ImageView controls in a layout file      Settextviewtext: Setting text for TextView controls in a layout file Remoteviews objects have many similar methods that are used to set properties for the controls in the layout file, and their parameters are basically the same, the first parameter is the control's ID, and the second is the The value of the property is 4. Set a click event for a control in a Remoteviews object's layout file: From the above we cannot find a control through the Remoteviews object.        So if you want to set a click event for a control, we need to use the Remoteviews method: Setonclickpendingintent (that is, we need the Pendingintent object to do what we need to do when we click the control), and its argument list is: int VIEWID: The ID of the control that needs to set the Click event Pendingintent pendingintent: This object encapsulates the action we need to complete when we click on the control, and we typically create a Pendingintent object to start the broadcast receiver. To handle our Click events in the broadcast recipient (that is, we also have to customize a broadcast receiver to handle the Click event, the required data is carried by pendingintent) and then we go back to the service: sometimes we need to know the work of the service (such as opening the service to complete the download work, Need to notify the user of the progress of the download, this is the service we can set a long-standing notification bar notification, to show the work, to achieve this effect, as long as we create a resident notification bar in the service notification and invoke the service Startforeground method, The parameter list is: INT ID: Notification ID Notification Notification: Notification to be displayed follow the public number: Apes gather here to get more articles.

Using the notification bar in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.