Android user interface-status bar notification (1)

Source: Internet
Author: User

Status bar notifications)

The status bar icon adds an icon to the system's status bar (with an optional text reminder message) and adds a notification message in the notification window. When you select this notification, the android system will trigger an intent object defined by the notification (usually to start an activity ). You can also configure sound, vibration, and screen flashes on the device to remind users.

Status bar notifications should be used in scenarios where background services require users to respond to related events. In order to accept user interaction, the background service should not start the activity by itself. On the contrary, the Service should create a status bar notification. When the user selects this notification, the corresponding activity is started.

Figure 1 shows a status bar with a notification icon on the left of the status bar.

Figure 1. Status Bar with notification

Figure 2 shows the notification messages in the notification window.

Figure 2. Notification Window

Basic

The activity or service object can initialize a status bar notification. Because the activity can only be executed when the foreground is running and the window has focus, the service object is usually used to create status bar notifications. In this way, when a user is using another application or device to sleep, the notification can also be created from the background. To create a notification, you must use two classes: Notification and icationicationmanager.

Use an instance of the notification class to define the attributes of the status bar notification, such as the status bar icon, notification message, and other settings such as playing sound. Icationicationmanager is a service of the Android system that executes and manages all status bar notifications. You do not need to instantiate the icationicationmanager object directly. To send the notification to it, you must use the getsystemservice () method to obtain the notificationmanager object reference, and then use the Y () method to pass the notification object to it when you want to notify the user.

The following describes how to create a status bar notification:

1. Obtain the notificationmanager object reference:

String NS = context. icationication_service;

Icationicationmanager mnotificationmanager = (notificationmanager) getsystemservice (NS );

2. initialize the notification object:

Int icon = R. drawable. icationication_icon;

Charsequence tickertext = "hello ";

Long when = system. currenttimemillis ();

Notification = new notification (icon, tickertext, when );

3. Define notification messages and pendingintent objects:

Context context = getapplicationcontext ();

Charsequence contenttitle = "my notification ";

Charsequence contenttext = "Hello world! ";

Intent icationicationintent = new intent (this, myclass. Class );

Pendingintent contentintent = pendingintent. getactivity (this, 0, icationicationintent, 0 );

Notification. setlatesteventinfo (context, contenttitle, contenttext, contentintent );

4. Pass the notification object to icationicationmanager:

Privatestaticfinalint hello_id
= 1;

Mnotificationmanager. Y (hello_id,
Notification );

So far, the user has been notified.

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.