Original link: http://www.orlion.ga/663/
1. Basic usage of notifications
To create a notification, you first need a notificationmanager to manage the notification, which can be obtained by invoking the Getsystemservice () method of the context. The Getsystemservice () method receives a string parameter to determine which service gets the system, and here we pass in Context.notification_service, So getting an instance of Notificationmanager can be written as:
Notificationmanager manager = (Notificationmanager) getsystemservice (Context.notification_service);
The next step is to create a notification object, which is used to store the various information required by the notification, which we can create using its parametric constructor. Notification the parameter constructor receives three parameters, the first parameter is used to specify the icon of the notification, the second parameter is used to specify the ticker content of the notification, when the notification has just been created, it will flash through the status bar of the system, belonging to an instantaneous prompt information. The third parameter specifies the time, in milliseconds, at which the notification is created, and the time specified here is displayed on the corresponding notification when the status bar is pulled, so creating a notification object can be written as:
Notification Notification = new Notification (R.drawable.icon, "This is ticker text", System.currenttimemillis ());
After creating the notification object, we also need to set the layout of the notification, where only the notification Setlastesteventinfo () method can be called to set a standard layout for the notification. This method receives four parameters, the first parameter is the context, the second parameter specifies the title of the notification, and the drop-down system status bar will be able to see the content. The third parameter is used to specify the body content of the notification, the same drop-down system status bar can see this part, the fourth parameter is temporarily not used, set to NULL, so the layout of the notification can be written as:
Notification.setlastesteventinfo (Context, "This is the content title", "This is content text", null);
Then just call Notificationmanager's Notify () method to let the notification display, the method receives two parameters, the first parameter is the ID, to ensure that the ID specified for each notification is different. The second parameter is a notification object, so the notification is displayed:
Manager.notify (1, notification);
This time can display the notice, but this notice is not clickable, if you want to achieve the click Effect need to use pendingintent. Pendingintent look like intent in their names, and there is a lot of common ground between them. They can, for example, specify an "intent" that can be used to initiate activities, start services, and send broadcasts. The difference is that intent is more inclined to perform an action immediately, while Pendingintent is more inclined to perform an action at a suitable time. Therefore, pendingintent can also be simply understood as deferred execution of the Intent.
Pendingintent is also very simple to use, it mainly provides several static methods for obtaining pendingintent instances, depending on the requirements to choose whether to use the Getactivity () method, Getbroadcast () method, or GetService () method. The parameters received by these methods are the same, the first parameter is still the Context, do not have to explain more. The second parameter is generally not used, and is usually passed in 0. The third parameter is a intent object, which allows us to construct a pendingintent "intent". The fourth parameter is used to determine the behavior of the pendingintent, with the four values of Flag_one_shot, Flag_no_create, Flag_cancel_current, and flag_update_current optional
Go back and look at Notification's Setlatesteventinfo () method. We have just ignored the fourth parameter of the Setlatesteventinfo () method, passed directly to null, and now look closely and find that the fourth parameter is exactly a Pendingintent object. As a result, a deferred execution "intent" can be built through pendingintent, and the logic is executed when the user clicks on the notification. Code:
Notificationmanager manager = (Notificationmanager) getsystemservice (Notification_service); Notification Notification = new Notification (R.drawable.ic_launcher, "This is ticker text", System.currenttimemillis ()) ; Intent Intent = new Intent (this, notificationactivity.class); pendingintent pi = pendingintent.getactivity (this, 0, intent,pendingintent.flag_cancel_current); Notification.setlatesteventinfo (This, "This is the content title", "This is content text", pi); manager.notify (1, notification);
First, we use intent to express our intention to start notificationactivity, and then pass the constructed intent object into the Pendingintent getactivity () method to get Pendingintent, and then passes it as a fourth parameter into the notification Setlatesteventinfo () method.
The notification will not disappear after clicking the notification, you need to cancel the notification in the code, you can call Notificationmanager's Cancel () method in Notificationactivity to cancel:
Notificationmanager manager = (Notificationmanager) getsystemservice (Notification_service); Manager.cancel (1);
As you can see, here we pass 1 in the Cancel () method, what does this 1 mean? Remember to give each notification the ID you specified when creating the notification? The ID we set for this notification was 1. So, if you want to cancel which notification, just pass in the ID of the notification in the Cancel () method.
2. Advanced Tips for Notifications
Observing the notification class, you will find that there are many properties that we have not used. Let's take a look at the sound property, which can play a piece of audio when the notification is issued, so that you can better inform the user that a notification is coming. Sound This property is a URI object, so you need to get the URI for the audio file when you specify the audio file. For example, there is a Basic_tone.ogg audio file in the/system/media/audio/ringtones directory of our phone, so this can be specified in the code:
Uri Sounduri = uri.fromfile (New File ("/system/media/audio/ringtones/basic_tone.ogg")); notification.sound = SoundUri;
In addition to allowing audio playback, we can also let the phone vibrate when the notification arrives, using the vibrate property. It is an array of long integers that sets the length of time, in milliseconds, for the phone to rest and vibrate. The value of subscript 0 indicates the length of the phone at rest, the value of subscript 1 indicates the length of the phone's vibration, and the value of 2 indicates the duration of the phone's inactivity, and so on. So, if you want your phone to vibrate for 1 seconds at the time of notification, then rest for 1 seconds and then vibrate for 1 seconds, the code can be written as:
Long[] vibrates = {0, +, +, 1000};notification.vibrate = vibrates;
To control your phone's vibrations, you also need to declare permissions:
<uses-permission android:name= "Android.permission.VIBRATE"/>
Let's take a look at how to control the display of your phone's LED lights when notifications come in. Now the phone is basically pre-set a LED light, when there are missed calls or unread text messages, and at this time the phone is in the lock screen state, LED lights will continue to blink, remind users to check. We can use the Ledargb, Ledonms, LEDOFFMS, and flags properties to achieve this effect. Ledargb is used to control the color of LED lights, generally with red, green and blue colors are optional. The LEDONMS is used to specify the length of time, in milliseconds, that the LED light is lit. The LEDOFFMS is used to specify the length of time the LED light goes dark, and also in milliseconds. Flags can be used to specify some of the behavior of the notification, where
Includes an option to display the LED light. So, when the notification arrives, if you want to achieve the LED lights with green lights flashing a flash of effect, you can write:
Notification.ledargb = Color.green;notification.ledonms = 1000;NOTIFICATION.LEDOFFMS = 1000;notification.flags = Notification.flag_show_lights;
Of course, if you do not want to make so many miscellaneous settings, you can also directly use the default effect of the notification, it will be based on the current phone environment to determine what the ringtone play, and how to vibrate, the following:
Notification.defaults = Notification.default_all;
Getting Started with Android (15) Notifications