Basic usage of wirelessly notification (Notification)

Source: Internet
Author: User

1. Use of notifications

Notifications can be implemented when an application wants to send a message to the user that the application is not running in the foreground. When a notification is issued, a notification icon appears in the status bar at the top of the phone, and the details of the notification are visible after the drop-down status bar.

2. steps to create a notification

(1) obtain the Notificationmanager instance, which can be obtained by invoking the Getsystenservice() method of Conten, The Getsystemservice () method receives a string parameter to determine which service gets the system, and here we pass in Context.notification_service . Get The Notificationmanager instance as follows:

Notificationmanager manager = (Notificationmanager) getsystemservice (Notification_service);

(2) Create the Notification object, which is used to store the various required information for the notification, which we can create using its parameter constructor. The constructor has three parameters, the first parameter specifies the notification icon, the second parameter specifies the ticker content of the notification, and when the notification has just been created, it flashes over the status bar of the system, which is a momentary hint of information. The third parameter specifies when the notification is created, in milliseconds, when the system status bar is pulled, and the time specified here is displayed on the corresponding notification. Creating a Notification object can be written as:

Notification Notification = new Notification (R.drawable.ic_launcher, "This is a ticker text", System.currenttimemillis () );

(3) call Notification 's Setlatesteventifo () method to set the layout of the notification, this method receives four parameters, the first parameter is the Context . The second parameter is used to specify the title of the notification, and the drop-down system status bar is where you can see the content. The third parameter is used to specify the body content of the notification, which can be seen in the same drop-down system status bar. The fourth parameter specifies the Pendingintent object that implements the notification click event , and if it is temporarily unavailable, it can pass null first . Therefore, the layout of the notification can be programmed to write:

Notification.setlatesteventinfo (Context, "This is the content title", "This iscontent text", null);

(4) call Notificationmanager 's notify() method to display the notification. the Notify () method receives two parameters, the first parameter is the ID, and the ID that isspecified for each notification is different. The second parameter is the Notification object, which directly passes in the Notification object we just created . Displaying a notification can be written as:

Manager.notify (1, notification);

3. code Examples

public class mainactivity extends activity implements onclicklistener {     private Button sendNotice;     @Override      protected void oncreate (bundle savedinstancestate)  {         super.oncreate (savedinstancestate);         Setcontentview (R.layout.activity_main);        sendnotice =  (Button)  findviewbyid (r.id.send_notice);         Sendnotice.setonclicklistener (This);    }     @Override      public void onclick (View view)  {         switch  (View.getid ())  {             case r.id.send_notice:   &nbSp;            notificationmanager manager  =  (Notificationmanager)  getsystemservice (notification_service);                 Notification notification =  New notification (                         R.drawable.ic_launcher,  "This is a  ticker text ",  system.currenttimemillis ());                                  notification.setlatesteventinfo (this,  "This is content  title ",                          "This is content text",  null);                 manager.notify (1, notification);             default:                 break;        }     }}


In addition, I am in the end of development APP will use some APP test the online Automated test tool: www.ineice.com


Basic usage of wirelessly notification (Notification)

Related Article

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.