Android getting started (XV) Notice

Original link: http://www.orlion.ga/663/

1. Basic usage of notification

To create a notification, you first need a notificationmanager to manage the notification, which can be obtained by calling the getsystemservice () method of context. The getsystemservice () method receives a string parameter to determine which service to obtain from the system. Here we pass in context. Notification_ Service is enough, so the instance of notificationmanager can be written as:

notificationmanager manager = (notificationmanager) getSystemService(Context.NOTIFICATION_SERVICE);

Next, we need to create a notification object, which is used to store all kinds of information required for notification. We can use its parametric constructor to create it. The parameterized constructor of notification receives three parameters. The first parameter is used to specify the icon of the notification, and the second parameter is used to specify the ticker content of the notification. When the notification is just created, it will flash in the system status bar, which is an instantaneous prompt information. The third parameter is used to specify the time when the notification is created, in milliseconds. When the status bar is pulled down, the time specified here will be displayed on the corresponding notification. Therefore, 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. Here, we only need to call the setlasteventinfo () method of notification to set a standard layout for the notification. This method receives four parameters. The first parameter is context, and the second parameter is used to specify the title content of the notification, which can be seen from the drop-down system status bar. The third parameter is used to specify the body content of the notification, which can be seen by pulling down the system status bar. The fourth parameter is temporarily unavailable and set to null. Therefore, setting the layout of the notification can be written as follows:

notification.setLastestEventInfo(context , "This is content title" , "This is content text" , null);

Then, the notification can be displayed by calling the notify () method of notificationmanager. This method receives two parameters. The first parameter is ID. ensure that the ID specified for each notification is different. The second parameter is the notification object, so the notification is displayed:

manager.notify(1 , notification);

The notification can be displayed at this time, but this notification is not clickable. To achieve the click effect, you need to use pendingintent. Pendingintent looks similar to intent in name, and they do have a lot in common. For example, they can all indicate a "intention" and can be used to start activities, start services and send broadcasts. The difference is that intent is more inclined to perform an action immediately, while pending intent is more inclined to perform an action at a suitable time. Therefore, pendingintent can also be simply understood as an intent that delays execution.

The usage of pendingintent is also very simple. It mainly provides several static methods to obtain the instance of pendingintent. You can choose whether to use getactivity() method, getbroadcast() method or getservice() method according to your needs. The parameters received by these methods are the same. The first parameter is still context. There is no need to explain. The second parameter is generally not used. It is usually passed in 0. The third parameter is an intent object through which we can construct the "intention" of pendingintent. The fourth parameter is used to determine the behavior of pendingintent, including flag_ ONE_ SHOT、 FLAG_ NO_ CREATE、 FLAG_ CANCEL_ Current and flag_ UPDATE_ The four values of current are optional

Let's go back to the setlatesteventinfo () method of notification. Just now, we ignored the fourth parameter of setlatesteventinfo () method and directly passed in null. Now take a closer look and find that the fourth parameter is a pendingintent object. Therefore, we can build a delayed execution "intention" through pendingintent, and the corresponding logic will be executed when the user clicks 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 content title","This is content text", pi);
manager.notify(1, notification);

Here, we first use intent to express our "intention" to start notificationactivity, then pass the constructed intent object into the getactivity () method of pendingintent to get the instance of pendingintent, and then pass it as the fourth parameter into the setlatesteventinfo () method of notification.

At this point, the notification will not disappear after clicking the notification, and the notification should be canceled in the code. The cancel () method of notificationmanager can be called in NotificationActivity to cancel:

notificationmanager manager = (notificationmanager)
getSystemService(NOTIFICATION_SERVICE);
manager.cancel(1);

As you can see, here we pass in 1 in the cancel () method. What does this 1 mean? Remember the ID assigned to each notification when creating the notification? At that time, the ID we set for this notification was 1. Therefore, if you want to cancel a notification, just pass in the ID of the notification in the cancel () method.

2. Advanced tips for notification

By observing the notification class, you will find that there are many properties we have not used. Let's take a look at the sound attribute. It can play an audio when the notification is sent, so as to better inform the user of the arrival of the notification. The attribute sound is a URI object, so when specifying an audio file, you also need to get the URI corresponding to the audio file first. For example, there is a basic in the / system / media / audio / ringtones directory of our mobile phone_ Tone.ogg audio file, you can specify the following 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 make the mobile phone vibrate when the notification comes. We use the vibrate attribute. It is a long integer array, which is used to set the static and vibration time of the mobile phone, in milliseconds. A value with a subscript of 0 indicates the static time of the mobile phone, a value with a subscript of 1 indicates the vibration time of the mobile phone, a value with a subscript of 2 indicates the static time of the mobile phone, and so on. Therefore, if you want the mobile phone to vibrate for 1 second immediately when the notification arrives, then stand still for 1 second and vibrate for 1 second, the code can be written as:

long[] vibrates = {0, 1000, 1000};
notification.vibrate = vibrates;

To control the vibration of the mobile phone, 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 mobile phone led when the notification comes. The current mobile phone basically has an LED light in front. When there is a missed call or unread message, and the mobile phone is in the locked screen state, the LED light will keep flashing to remind the user to check. We can use ledargb, ledonms, ledoffms and flags to achieve this effect. Ledargb is used to control the color of LED lights. Generally, there are three colors: red, green and blue. Ledonms is used to specify how long the LED lights up, in milliseconds. Ledoffms is used to specify the duration of LED dimming, also in milliseconds. Flags can be used to specify some behavior for notifications, where

This includes the option to display LED lights. Therefore, when the notice arrives, if you want to realize the effect of LED light flashing with green light, you can write as follows:

notification.ledARGB = Color.GREEN;
notification.ledOnMS = 1000;
notification.ledOffMS = 1000;
notification.flags = Notification.FLAG_SHOW_LIGHTS;

Of course, if you don't want to make so many complicated settings, you can also directly use the default effect of notification. It will decide what ring tone to play and how to vibrate according to the current mobile phone environment. The writing method is as follows:

notification.defaults = Notification.DEFAULT_ALL;

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>