Status bar notifications for Android Study Notes

Source: Internet
Author: User

<1> Introduction

There is an innovative UI design in the status bar of the Android system, which is a notification prompt that can be pulled down. When the system has some messages to notify users, such as receiving text messages, emails, or missed calls, the information will be sent to users as notification.

The status bar adds an icon to the system status bar and text information (Optional). The notification information is added to the notification window. You can also install the sound, vibration, and flashing lights used by notification to remind users.

 

Both notification and toast can serve as notifications and reminders. However, their implementation principles and forms are completely different.

Toast is actually equivalent to a widget ). Some dialog boxes are similar to those without buttons. Notification is the information displayed in the status bar above the screen.

Notification must be managed by icationicationmanager, while toast only needs to create a toast object.


<2> steps:

Create status bar notification:

1. Icationicationmanager for a reference:

String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

Or

NotificationManager mNotificationManager  = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

2. Create a notification object. Each notification corresponds to a notification object. In this step, you need to set the notification message displayed on the status bar above the screen, the image resource ID in front of the notification message, and the time when the notification is sent. It is generally the current time.

 

Int icon = R. drawable. icationication_icon; // The icon displayed in the status bar above the screen
Charsequence tickertext = "hello"; // notification message displayed in the status bar above the screen
Long when = system. currenttimemillis (); // time when the notification is sent
Notification notification = new Notification(icon, tickerText, when);

3. Defined information and pendingintent notifications:

Because notification can be detached from the application. That is to say, even if the application is closed, notification will still be displayed in the status bar. After the application is started again, you can re-control these notifications. Such as clearing or replacing them. Therefore, you must create a pendingintent object. This object is maintained by the Android system. Therefore, the object will not be released after the application is closed.
Use the setlatesteventinfo method of the notification class to set notification details.

Context context = getApplicationContext();
Charsequence contenttitle = "my notification"; // Notification title
Charsequence contenttext = "Hello world! "; // Notification content
Intent icationicationintent = new intent (this, myclass. Class); // jump to myclass. Class
Pendingintent contentintent = pendingintent. getactivity (this, 0, icationicationintent, 0); // generate a pendingintent
Notification. setlatesteventinfo (context, contenttitle, contenttext, contentintent); // very important method

4. Notification to icationicationmanager:

Use the y icationmanager class's notify y method to display notification messages. In this step, you must specify the unique ID that identifies the notification. This ID must be unique relative to the same notificationmanager object; otherwise, it will overwrite the same ID's notificaiton ()

private static final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);

Here, we need to explain the 1st parameters of the notify method. This parameter actually represents the notification ID. Is an int type value.

To make this value unique, you can use some resource IDs in the res directory. For example, in the code above, the resource ID (R. drawable. Icon) corresponding to the image displayed by the current notification is used as the notification ID. Of course, readers can also use other values as notification ID values.

5. You can also set the default voice, vibration, and light effects when the notification is displayed. To implement this function, you need to set the ults attribute of the notification class. The Code is as follows:

1 Add sound:

You can remind the user to provide the default notification sound (which is defined by the user) or application-specific sound.

Application-defined sound:

   notification.defaults = Notification.DEFAULT_SOUND;

Custom: a different sound notification is used to reference the sound location through a URI. The following example uses an audio file known to the SD card that is saved to the device:

notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");

2 Increase vibration:

You can remind users to provide vibration (which is defined by users) or application-specific vibration modes.
Application-defined vibration mode:

notification.defaults = Notification.DEFAULT_VIBRATE;

Define your own vibration mode through a series of long values:

long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate;

The first value is the length of the first vibration, the third is the next length, and so on. The pattern can be set as repeated as long as you like it.

3 add flashing light

Notify the user of flashing LED lights. You can inherit the default T mode (if any) or define your own color and pattern lights.

Use the default lighting settings, and add default_lights to defaults:

notification.defaults = Notification.DEFAULT_LIGHTS;

 

<3> more functions (using flag)
notification.flags = Notification.FLAG_AUTO_CANCEL;

After the notification is automatically canceled, This is the notification selection window.

notification.flags = Notification.FLAG_INSISTENT; 

Repeat until the user responds.

notification.flags = Notification.Flag_no_clear;

Indicates that the notification should not be cleared by the "Clear notifications" button. This is especially useful if your notification is being notified.

 

 

Example:

Package xiaosi. notification; import android. app. activity; import android. app. notification; import android. app. icationicationmanager; import android. app. pendingintent; import android. content. intent; import android. OS. bundle; public class icationicationactivity extends activity {/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); icationicationmanager notificationmanager = (notificationmanager) getsystemservice (icationication_service); Notification = new notification (R. drawable. a, "you have a short message", system. currenttimemillis (); notification. flags = notification. flag_auto_cancel; intent icationicationintent = new intent (); pendingintent pedingintent = pendingintent. getactivity (this, 0, icationicationintent, 0); notification. setlatesteventinfo (this, "you have a short message", "wish you a Happy Wedding", pedingintent); icationicationmanager. notify (R. drawable. a, notification );}}

 

package xiaosi.notification;import android.app.Activity;import android.os.Bundle;public class MainActivity extends Activity{/** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);    }}

Main. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <textview Android: id = "@ + ID/start" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "Notification Demo"/> </linearlayout>

 

 

 

 

 

 

Click to download source code: source code

 

 

 

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.