When an application wants to send some hints to the user, and the application is not in the foreground, it can be implemented with notification. After a notification is issued, the top notification bar of the phone will display an icon, and the status bar can see the details later.
I. Basic usage of notifications
Notifications can be created in an activity or in a broadcast sink, and can be created in a service.
Steps to issue a notification:
1.1 Get the Notification Manager class instance:
Notificationmanager manager = Getsystemservice (context.notification_service);
1.2 Build Notifications
Using Notification.builder to construct a notification, the sample code is as follows:
NotificationNoti= New Notification.Builder(Mcontext)
.Setcontenttitle("New Mail from" +Sender.tostring ())
setcontenttext (subject
. Setsmallicon (r. Drawable. New_mail)
. Setlargeicon (abitmap)
.
1.3 After you create a notification, you also need to set the notification, such as title, content, layout, etc. Setlatesteveninfo () method can be a notification to set a standard layout, the first parameter is the context, the second parameter is the tittle content, the third parameter is the body content, the fourth parameter does not know what to do, pass a null.
1.4 Call Manaager's Notify () method to send the notification out. For example: Notify (1,notification); The first parameter is the ID of the notification, the ID of each notification should be guaranteed to be different, and the second parameter is the notification we built.
Second, open other applications through notification
With Pendingintent implementation, pendingintent is similar to intent and can specify an intent, such as initiating an activity, starting a service, initiating a broadcast, and so on, unlike intent's tendency to execute immediately, The two pendingintent are more inclined to perform an action in a suitable reality.
Pendingintent mainly provides several static methods for obtaining instances of pendingintent, such as getactivity (),
In the build notification, the defined pendingintent is passed into the ntification.builder.
Basic methods of using notification