How to use notification notification in Android _android

Source: Internet
Author: User

Everyone using the Android phone should be familiar with the Android notice, so let's learn how to use the Notifications on Android.

The basic usage of a notice

Notifications can be created in activities, broadcast receivers, and services, and since we typically use notifications only after the program has entered the background, it is generally rare to create notifications in the event.

1, the first line of code described above to create the notification method

Get Notification Manager
Notificationmanager Manager = (Notificationmanager) getsystemservice (Context.notification_service)
//Create a notification object, which is the notification icon, ticker (a flashing message on the notification bar), notification creation time
Notification Notification = new Notification (r.drawable. Ic_launcher, "This is ticker text", System.currenttimemillis ()) ;
Set the notification layout, followed by the context, the notification title, the notification body, the Pindingintent object (event handling after clicking the notification)
Notification.setlatesteventinfo (This, "this is Content title "," This is content text ", null);
Displays notifications, which are sequentially unique IDs, notification objects
manager.notify (1, notification);

Note: The above method is now deprecated, using this method when the API level is 11 and before

2, APILevel more than 11 less than 16 can be used to create alerts in the following ways

1. Get Notification Manager
Notificationmanager Manager = (Notificationmanager) getsystemservice (Context.notification_service) ;
Create Builder, set properties
notification.builder Builder = new Notification.builder (this)
    . Setautocancel (True)
    . Setcontenttitle ("title")
    . Setcontenttext ("describe").
    Setsmallicon (r.drawable.ic_launcher)
    . Setwhen (System.currenttimemillis ())
    . Setongoing (true);
Get Notification object
Notification Notification = builder.getnotification ();
Display Notification
manager.notify (1, notification);

3, API level in 16 and above, use the following method to create alerts

1. Get Notification Manager
Notificationmanager Manager = (Notificationmanager) getsystemservice (Context.notification_service) ;
Create Builder, set properties
Notification Notification = new Notification.builder (this)
    . Setautocancel (True)
    . Setcontenttitle ("title")
    . Setcontenttext ("describe").
    Setsmallicon (r.drawable.ic_launcher)
    . Setwhen (System.currenttimemillis ())
    . Setongoing (True)
    . Build ();
Display Notification
manager.notify (1, notification);

Second, the response to the notification of the Click event

We passed the Pendingintent object response to the Click event of the volume notification
1. Get Pendingintent Object

Pendingintent is used to process the "intent" of the notification. We need to first construct a intent object and then pass Pendingintent.getactivity (), Pendingintent.gbroadcast (), Pendingintent.getservice () To initiate the execution of different intents. The three static methods pass in the same parameters, the first is the context, the second parameter is generally passed in 0, the third parameter is the intent object, and the fourth parameter specifies the behavior of the pendingintent, which has flag_one_shot, Flag_no_create, The four values of flag_cancel_current and Flag_update_ current are optional.

2, set Pendingintent

Set by Setcontentintent (Pendingintent).

The following is a simple example

Get Notification Manager
Notificationmanager Manager = (Notificationmanager) getsystemservice (context.notification_service);
Construct Intent Object
Intent Intent = new Intent (mainactivity.this, testactivity.class);
Get Pendingintent object
pendingintent pendingintent = pendingintent.getactivity (this, 0, intent, pendingintent.flag_ cancel_current);
Create Builder, set properties
Notification Notification = new Notification.builder (this)
    . Setautocancel (True)
    . Setcontenttitle ("title")
    . Setcontenttext ("describe").
    Setsmallicon (r.drawable.ic_launcher)
    . Setcontentintent (pendingintent)  //Set pendingintent
    . Setwhen (System.currenttimemillis ())
    . Setongoing (True)
    . Build ();
Display Notification
manager.notify (1, notification);

Iii. Cancellation of notice

Canceling notifications requires only the ID specified in the Cancel () method when we create the notification

Copy Code code as follows:
Notificationmanager manager = (Notificationmanager) getsystemservice (Context.notification_service);
Manager.cancel (1);

Iv. Advanced usage of notifications

1. Play audio when notice arrives

Notification has a property that is sound, where you need to pass in the corresponding URI of the audio

 Audio uri
uri Sounduri = uri.fromfile (New File ("/system/media/audio/ringtones"));
Setsound (Sounduri);

2. Mobile phone vibration when notice arrives

We use the vibrate property to control the vibration of the phone when the notification arrives. Vibrate requires a long integer array to set the length of time the phone is still and vibrate in milliseconds. An even-numbered sign indicates that the cell phone is still long and is labeled odd for the length of the phone's vibration.

 Mobile phone vibration still set (still 0 seconds, vibration one second, still one second, vibration one second)
long[] vibrate = {0, 1000, 1000, 1000};
Setvibrate (Vibrate)

Note: Control of mobile phone also need to declare permissions in the Androidmanifest.xml:

<uses-permission android:name= "Android.permission.VIBRATE"/>

3, notify the arrival of flashing LED lights

The use of LED lights involves one of the following properties:
Ledargb ——-Control The color of LED lights
Ledonms ——-control when the LED light is on, in milliseconds
ledoffms--– control LED lights off time, in milliseconds
These three properties are set primarily through the Setlights () method

Setlights (Color.Blue, 1000, 1000)

The code above is to let the LED lights flash in blue
4, notify the arrival of the default way to remind

If we don't want to set so many properties manually, we can use the following method
. SetDefaults (Notification.default_all)

Set the default value, the mobile phone environment to decide what to play when the notice comes, how to vibrate, how flashing LED lights
Finally, a point, mobile phone playback ring, mobile phone vibration, LED lights flashing all need to real machine debugging, the simulator is not to see the effect.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.