About the use of notification requires importing 3 classes
1 2 3import android.app.PendingIntent; Import Android.app.NotificationManager; Import android.app.Notification;
Code Examples and Descriptions
1 2 3 4 5 6 7 8 9 ten-18NotificationManager nm = (Notificationmanager) getsystemservice (context.notific Ation_service);
Notification n =
New Notification (R.drawable.chat,
"Hello,there!",
System.currenttimemillis ()); N.flags = Notification.flag_auto_cancel;
Intent i =
New Intent (Arg0.getcontext (),
Notificationshow.class); I.setflags (intent.flag_activity_clear_top| Intent.flag_activity_new_task);
Pendingintent pendingintent contentintent = pendingintent.getactivity (Arg0.getcontext (),
R.string.app_name,
I
Pendingintent.flag_update_current); N.setlatesteventinfo (Arg0.getcontext (), "hello,there!",
"Hello,there,i ' m
John. ",
Contentintent); Nm.notify (R.string.app_name, N);
Each piece of code is analyzed in turn:
1NotificationManager nm = (Notificationmanager) getsystemservice (Context.notification_service);
Creates a notificationmanager that creates a NM object responsible for "emitting" and "canceling" Notification.
1 2Notification n =
New Notification (R.drawable.chat,
"Hello,there!",
System.currenttimemillis ()); N.flags = notification.flag_ongoing_event;
Create Notification, with the parameters: Icon's resource ID, scrolling information displayed on the status bar, time. The N object that is created is used to describe the information that appears in the system notification bar, and we will then see the intent that is set on the N object to click on this notification.
1n.flags = Notification.flag_auto_cancel;
Set N.flags to Notification.flag_auto_cancel, which indicates that the notification can be cleared when the user clicks clear.
1 2Intent i =
New Intent (Arg0.getcontext (),
Notificationshow.class); I.setflags (intent.flag_activity_clear_top| Intent.flag_activity_new_task);
Create a intent that intent make this intent when the user clicks the notification
Note that if you want to start an ACTIVITY with the Intent, be sure to set the Intent.flag_activity_new_task tag.
Intent.flag_activity_clear_top: If there is activity to start in the current task, turn off all activity before the acitivity, and to avoid creating instances of the activity by placing this activity before
Intent.flag_activity_new_task: The system checks to see if there is a task for the activity that is being started in all the currently created tasks, and if so, creates an activity on that task, If not, a new task with the Activity property is created, and an activity on the new task.
1 2 3 4 5 6//pendingintent pendingintent contentintent = pendingintent.getactivity (Arg0.getcontext (),
R.string.app_name,
I
Pendingintent.flag_update_current);
Pendingintent for Intent packaging, here is a description of the start intent, Pendingintent.getactivity returns the Pendingintent said, The intent in this pendingintent instance is the intent used to start the Activity. The parameters of the pendingintent.getactivity are: Context, the sender's request code (can fill 0), for the system to send the intent, flag bit.
Where pendingintent.flag_update_current indicates that if the pendingintent of the description already exists, The extra data for the existing pendingintent is changed to the extra data for the new pendingintent.
Here is a brief talk about the difference between Intent and pendingintent:
Intent: The intention, that is to tell the system what I want to do, and then the system according to this Intent do the corresponding thing. such as startactivity is equivalent to sending a message, while intent is the content of the message.
Pendingintent: Packaging Intent,intent is our intention to use startactivity, StartService, or sendbroadcast to start a work directly. At some point, we can not call startactivity, startservide or sendbroadcast directly, but send intent when a program or system reaches a certain condition. As notification here, when the user clicks Notification, the system issues an activity Intent. So if we don't have a way to tell the system, the system doesn't know whether to use StartActivity, StartService or Sendbroadcast to start intent (and of course other "descriptions"), So here we need pendingintent.
1 2 3 4 5n.setlatesteventinfo (Arg0.getcontext (), "hello,there!",
"Hello,there,i ' m
John. ",
Contentintent);
Set the information displayed in the Notification drop-down box, with the following parameters: Context, title, content, Pendingintent.
1nm.notify (R.string.app_name, N);
Start notification with the following parameters: Identify the ID value of the notification in your program (used to distinguish between different notifycation in the same program, if there is only one notification in the program then you can fill anything you want, However the type must be int), the notification to be notified.
How to make your own notification like Android QQ can appear under the "Running" column
Actually very simple, just set notification.flags = Notification.flag_ongoing_event;
How to change the layout of Notification under the "Running" column
Create Remoteviews and assign it to Notification.contentview, and then assign Pendingintent to Notification.contentintent, such as:
1 2 3 4 5 6 7 8 9 13PendingIntent contentintent = pendingintent.getactivity (Arg0.getcontext (),
R.string.app_name, I,
Pendingintent.flag_update_current); Remoteviews RV =
New Remoteviews (Main.this.getPackageName (),
R.layout.notification_view); Rv.setimageviewresource (R.id.image, r.drawable.chat); Rv.settextviewtext (R.id.text,
"Hello,there,i ' M John."); N.contentview = RV; N.contentintent = contentintent; Nm.notify (R.string.app_name, N);
Note that if Contentview is used, then do not use Notification.setlatesteventinfo. If the setlatesteventinfo is assigned to the Notification.contentview code, then the Contentview effect will be overwritten, showing the effect of setlatesteventinfo; Setlatesteventinfo before the Notification.contentview code, then the Notification.contentview effect is displayed, That is, regardless of the custom effect you want to setlatesteventinfo or Contentview, make sure there is always only one set of code, because when the last sentence is bound, The previous setup of Contentview or Setlatesteventinfo code is completely unnecessary.
Article by: http://nk.39.net/shjl/nszx/jszx/index.html finishing release.
Introduction to the use of Android notification