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
- Package Cn.com.chenzheng_java;
- Import android.app.Activity;
- Import android.app.Notification;
- Import Android.app.NotificationManager;
- Import android.app.PendingIntent;
- Import Android.content.Context;
- Import android.content.Intent;
- Import Android.net.Uri;
- Import Android.os.Bundle;
- Import Android.provider.MediaStore.Audio;
- Import Android.view.View;
- Import Android.widget.Button;
- /***
- * @description status bar notification related
- * @author Chenzheng_java
- *
- */
- Public class Notificationactivity extends Activity {
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (r.layout.notification);
- Button button = (button) Findviewbyid (R.id.button);
- Button.setonclicklistener (new View.onclicklistener () {
- @Override
- public void OnClick (View v) {
- Addnotificaction ();
- }
- });
- }
- /**
- * Add a notification
- */
- private void Addnotificaction () {
- Notificationmanager manager = (notificationmanager) This
- . Getsystemservice (Context.notification_service);
- //Create a notification
- Notification Notification = new Notification ();
- //Set the icon that appears on the top of the phone's status bar
- Notification.icon = R.drawable.excel;
- //When the current notification is placed on the status bar, the prompt content
- Notification.tickertext = "Note that I was thrown into the status bar";
- /***
- * Notification.contentintent: A Pendingintent object that will be triggered when the user taps the icon on the status bar. Intent
- * Notification.contentview: We can not put an icon in the status bar but put a view
- * Notification.deleteintent Intent executed when the current notification is removed
- * Notification.vibrate when the phone vibrates, the vibration period is set
- */
- //Add sound tips
- Notification.defaults=notification.default_sound;
- The value of the //Audiostreamtype must be audiomanager in the value, which represents the mode of the bell
- Notification.audiostreamtype= Android.media.AudioManager.ADJUST_LOWER;
- ///below two ways to add music
- //notification.sound = Uri.parse ("File:///sdcard/notification/ringer.mp3");
- //notification.sound = Uri.withappendedpath (Audio.Media.INTERNAL_CONTENT_URI, "6");
- Intent Intent = new Intent (This, notification2activity. Class);
- Pendingintent pendingintent = pendingintent.getactivity (this, 0, intent, pendingintent.flag_one_shot);
- //Click the icon that appears on the status bar to set the prompt information
- Notification.setlatesteventinfo (This, "content tip:", "I am a test file", pendingintent);
- Manager.notify (1, notification);
- }
- }
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