The use of Notificationmanager

Source: Internet
Author: User

Code parsing


1. Create a Notification Manager

Notificationmanager is a system service that must be obtained through the Getsystemservice () method.


Notificationmanager Notificationmanager = (notificationmanager) getsystemservice (Context.notification_service);

2. Create a notification

Notification: You can set the icon, text, cue sound, vibration and so on parameters.


int icon = R.drawable.wlicon;

Long when = System.currenttimemillis ();//Time

Create a display of the notification bar

Charsequence tickertext = "unread message reminder";

Notification Notification = new Notification (icon, Tickertext, when);

Notification.defaults |= notification.default_lights; Notification Lights

Notification.defaults |= notification.default_vibrate; Vibration

Notification.flags |= notification.flag_no_clear; Notifications cannot be cleared

Notification.flags = Notification.flag_auto_cancel; Notifications can be cleared

Notification.sound = Ringtonemanager.getdefaulturi (ringtonemanager.type_notification); system default Ringtone

Notification.sound = Uri.parse ("File:///sdcard/notification/ringer.mp3");//Play Custom Ringtones

Notification.flags |= notification.flag_insistent; The sound always rings to the user, that is, the notification will always ring until you touch the notification bar and the time will stop.

Content that is notified in the status bar after creation

Context context = Droidgap.getapplicationcontext ();

Charsequence contenttitle = "unread message reminder";

Charsequence ContentText = "You have" + Quantity + "unread message, please read it in time. ";

Click to open a project to create a intent

Intent notificationintent = new Intent (Droidgap, Mainactivity.class);

Pendingintent contentintent = pendingintent.getactivity (droidgap, 0, notificationintent, 0);

Notification.setlatesteventinfo (context, Contenttitle, ContentText, contentintent);

Notificationmanager.notify (notification_id, NOTIFICATION);

Complete example


The ringing in the notification bar is not used here because the user pulls down the notification bar when that Bell stops and I need to play it all the time. So I used MediaPlayer.


/**

* Create a notification bar

* Number of @param Quantity

* @param player MediaPlayer play sound

*/

public void Createnotification (String Quantity, MediaPlayer player) {

Create a notification bar

Notificationmanager Notificationmanager = (notificationmanager) droidgap.getsystemservice (Context.NOTIFICATION_ SERVICE);

int icon = R.drawable.wlicon;

Long when = System.currenttimemillis ();

Create a display of the notification bar

Charsequence tickertext = "unread message reminder";

Notification Notification = new Notification (icon, Tickertext, when);

Notification.defaults |= notification.default_lights; Notification Lights

Notification.defaults |= notification.default_vibrate; Vibration

Notification.flags |= notification.flag_no_clear; Notifications cannot be cleared

Notification.flags = Notification.flag_auto_cancel; Notifications can be cleared

Notification.sound = Ringtonemanager.getdefaulturi (ringtonemanager.type_notification); system default Ringtone

Notification.sound = Uri.parse ("File:///sdcard/notification/ringer.mp3");//Play Custom Ringtones

Notification.flags |= notification.flag_insistent; The sound always rings to the user, that is, the notification will always ring until you touch the notification bar and the time will stop.

Content that is notified in the status bar after creation

Context context = Droidgap.getapplicationcontext ();

Charsequence contenttitle = "unread message reminder";

Charsequence ContentText = "You have" + Quantity + "unread message, please read it in time. ";

Click to open a project to create a intent

Intent notificationintent = new Intent (Droidgap, Mainactivity.class);

Pendingintent contentintent = pendingintent.getactivity (droidgap, 0, notificationintent, 0);

Notification.setlatesteventinfo (context, Contenttitle, ContentText, contentintent);

Notificationmanager.notify (notification_id, NOTIFICATION);

try {

if (Integer.parseint (Quantity) = = 0 && player.isplaying ()) {

Player.reset (); To the initialization state, here it is necessary to determine whether the bell is ringing, if the direct opening at once will appear 2 ringtones have been ringing, you do not believe you can try

} else if (!player.isplaying ()) {

Notificationutil.ring (player);

}

} catch (Exception e) {

E.printstacktrace (); To change body of catch statement use File | Settings | File Templates.

}

}

/**

* Always ringing

* @param droidgap

* @param player

* @return

* @throws Exception

* @throws IOException

*/

private static MediaPlayer ring (MediaPlayer player) throws Exception, IOException {

Uri alert = Ringtonemanager.getdefaulturi (ringtonemanager.type_notification);

MediaPlayer player = new MediaPlayer ();

Player.setdatasource (droidgap, alert);

Final Audiomanager Audiomanager = (audiomanager) getsystemservice (Context.audio_service);

if (Audiomanager.getstreamvolume (audiomanager.stream_notification)! = 0) {

Player.setaudiostreamtype (audiomanager.stream_notification);

Player.setlooping (TRUE);

Player.prepare ();

Player.start ();

}

return player;

}


The use of Notificationmanager

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.