Android Notification notifications

Source: Internet
Author: User

 Android Notification Notifications

When we use the mobile phone, if the text message, and we do not click to see, is not in the top of the mobile phone status bar has a small text icon hint ah? Do you want to implement this function? Today's notification is to solve this problem.

[Java]View Plaincopy
  1. Package Cn.com.chenzheng_java;
  2. Import android.app.Activity;
  3. Import android.app.Notification;
  4. Import Android.app.NotificationManager;
  5. Import android.app.PendingIntent;
  6. Import Android.content.Context;
  7. Import android.content.Intent;
  8. Import Android.net.Uri;
  9. Import Android.os.Bundle;
  10. Import Android.provider.MediaStore.Audio;
  11. Import Android.view.View;
  12. Import Android.widget.Button;
  13. /***
  14. * @description status bar notification related
  15. * @author Chenzheng_java
  16. *
  17. */
  18. Public class Notificationactivity extends Activity {
  19. @Override
  20. protected void OnCreate (Bundle savedinstancestate) {
  21. super.oncreate (savedinstancestate);
  22. Setcontentview (r.layout.notification);
  23. Button button = (button) Findviewbyid (R.id.button);
  24. Button.setonclicklistener (new View.onclicklistener () {
  25. @Override
  26. public void OnClick (View v) {
  27. Addnotificaction ();
  28. }
  29. });
  30. }
  31. /** 
  32. * Add a notification
  33. */
  34. private void Addnotificaction () {
  35. Notificationmanager manager = (notificationmanager) This
  36. . Getsystemservice (Context.notification_service);
  37. //Create a notification
  38. Notification Notification = new Notification ();
  39. //Set the icon that appears on the top of the phone's status bar
  40. Notification.icon = R.drawable.excel;
  41. //When the current notification is placed on the status bar, the prompt content
  42. Notification.tickertext = "Note that I was thrown into the status bar";
  43. /*** 
  44. * Notification.contentintent: A Pendingintent object that will be triggered when the user taps the icon on the status bar. Intent
  45. * Notification.contentview: We can not put an icon in the status bar but put a view
  46. * Notification.deleteintent Intent executed when the current notification is removed
  47. * Notification.vibrate when the phone vibrates, the vibration period is set
  48. */
  49. //Add sound tips
  50. Notification.defaults=notification.default_sound;
  51. The value of the //Audiostreamtype must be audiomanager in the value, which represents the mode of the bell
  52. Notification.audiostreamtype= Android.media.AudioManager.ADJUST_LOWER;
  53. ///below two ways to add music
  54. //notification.sound = Uri.parse ("File:///sdcard/notification/ringer.mp3");
  55. //notification.sound = Uri.withappendedpath (Audio.Media.INTERNAL_CONTENT_URI, "6");
  56. Intent Intent = new Intent (This, notification2activity.   Class);
  57. Pendingintent pendingintent = pendingintent.getactivity (this, 0, intent, pendingintent.flag_one_shot);
  58. //Click the icon that appears on the status bar to set the prompt information
  59. Notification.setlatesteventinfo (This, "content tip:", "I am a test file", pendingintent);
  60. Manager.notify (1, notification);
  61. }
  62. }

When you click the button, the status bar displays:

See, the status bar more than an Excel icon, when I hold the icon down, drag down the time, came out this page

Then, when we click on this dialog, it will trigger intent and jump to notification2activity.java activity.

----------------------------------------------------------------------------------------

Note that the ID in notify (id,notification) in Notificationmanager is the identifier that uniquely identifies our current notification, and this value is passed when we delete the notification through the Cancel method. Perhaps the reader is looking at a lot of documents, found this place to specify a strange strange value, such as R.drawable.icon, many friends wonder, why here to specify a picture it. Here the author of the introduction, why?

The answer is very simple, and we all know that the only thing we need to do with parameters here is that the ID is consistent with the Notify method and is unique, as long as the two items are met, the rest is irrelevant. Notify and Cancel in the same way we as developers, too good control, but the only thing, we really do not say, so here are some people move small brains, very clever use of our system of image resources or other resources index ID, we know that these values are definitely unique!

------------------------------------------------------------------------------------------

Here are some of the information you find online:

If you want to add a notification, you can follow these steps

1: Get Notificationmanager:

Notificationmanager m_notificationmanager= (Notificationmanager) This.getsystemservice (NOTIFICATION_SERVICE);

2: Define a notification:

Notification m_notification=new Notification ();

3: Set various properties of notification:

Set the icon that the notification displays in the status bar
M_notification.icon=r.drawable.icon;

What is displayed when we click on the notification
m_notification.tickertext= "Button1 notification content ...";

Default sound emitted at notification
M_notification.defaults=notification.default_sound;

Set parameters for notification display

Intent m_intent=new Intent (notificationdemo.this,desactivity.class);
Pendingintent m_pendingintent=pendingintent.getactivity (notificationdemo.this, 0, m_Intent, 0);

M_notification.setlatesteventinfo (Notificationdemo.this, "Button1", "Button1 notice", m_pendingintent);

This can be understood as starting to execute this notification
M_notificationmanager.notify (0,m_notification);

4: Since can be added likewise we can also delete. Of course it's just deleting your own additions.

M_notificationmanager.cancel (0);

Here 0 is an ID number, and notify the first parameter 0.

This is done, adding the deletion work.

------------------------------------------------------------------------------------------------------

Noticificationmanager can easily be placed in the status bar, it is easy to implement from the StatusBar into the program,
The activity of this program is performed through intent in Noticificationmanager.

Noticificationmanager Status bar Action

Notificationmanager (Notification Manager):
Notificationmanager is responsible for notifying users of the occurrence of events.
Notificationmanager has three public methods:
1. Cancel (int id) cancels a previously displayed notification. If it is a short notice, try to hide it, and if it is a persistent notification, it will be removed from the status bar.
2. Cancelall () cancels all previously displayed notifications.
3. Notify (int ID, Notification Notification) sends the notification to the status bar durably.


Initialize Notificationmanager:
Notificationmanager nm =
(Notificationmanager) Getsystemservice (Notification_service);

The notification represents a notification.
Properties of the notification:
Audiostreamtype the type of audio stream used when the sound is ringing
Contentintent executes this set of intent when the notification entry is clicked.
Contentview This set of views is displayed when the notification is displayed on the status bar.
defaults specifies which value is to be set to the default.
Deleteintent This set of intent is executed when the user clicks the "Clear All Notifications" button area to remove all notifications.
The picture used by the icon status bar.
Iconlevel if the picture of the status bar has several levels, set here.
Ledargb the color of LED lights.
LEDOFFMS Flash time (in milliseconds) when LED is off
Ledonms Flash time at start of LED (in milliseconds)
Number this notification represents the numbers of the events
Sounds of sound notifications
The information displayed when the TICKERTEXT notification is displayed in the status bar
Vibrate vibration mode.
Timestamp of when notification.

Send the notification to the status bar:
Notification Notification = new Notification ();
Notification setup process .....
Nm.notify (0, notification); Send to status bar

------------------------------------------------------------------------------------------------------------

Notification offers a wide range of tips for mobile phones:

A) A notification text prompt that appears in the status bar, such as:

Notification.tickertext = "Hello";

b) Beep tones, such as:

Notification.defaults = Notification.default_sound;

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

Notification.sound = Uri.withappendedpath (Audio.Media.INTERNAL_CONTENT_URI, "6");

c) mobile phone vibration, such as:

Notification.defaults = notification.default_vibrate;

Long[] vibrate = {0,100,200,300};

Notification.vibrate = vibrate;

d) flashing LED lights, such as:

Notification.defaults = notification.default_lights;

Notification.ledargb = 0xff00ff00;

NOTIFICATION.LEDONMS = 300;

NOTIFICATION.LEDOFFMS = 1000;

Notification.flags = notification.flag_show_lights;

4) Send notification:

private static final int id_notification = 1;

Mnotificationmanager.notify (Id_notification, NOTIFICATION);

Android Notification notifications

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.